note 66187 added to function.microtime

From: Date: Tue, 16 May 2006 19:47:52 +0000
Subject: note 66187 added to function.microtime
Groups: php.notes 
Request: Send a blank email to php-notes+get-111405@lists.php.net to get a copy of this message
Of the methods I've seen here, and thought up myself, to convert microtime() output into a numerical value, the microtime_float() one shown in the documentation proper(using explode,list,float,+) is the slowest in terms of runtime. I implemented the various methods, ran each in a tight loop 1,000,000 times, and compared runtimes (and output). I did this 10 times to make sure there wasn't a problem of other things putting a load spike on the server. I'll admit I didn't take into account martijn at vanderlee dot com's comments on testing accuracy, but as I figured the looping code etc would be the same, and this was only meant as a relative comparison, it should not be necessary. The above method took on average 5.7151877 seconds, while a method using substr and simply adding strings with . took on average 3.0144226 seconds. rsalazar at innox dot com dot mx's method using preg_replace used on average 4.1819633 seconds. This shows that there are indeed differences, but for normal use noone is going to notice it. Note that the substr method mentioned isn't quite the one given anonymously below, but one I made based on it: <?php $time=microtime(); $timeval=substr($time,11).substr($time,1,9); ?> Also worth noting is that the microtime_float() method gets faster, and no less accurate, if the (float) conversions are taken out and the variables are simply added together. Any of the methods that used + or array_sum ended up rounding the result to 2 digits after the decimal point, while (most of) the ones using preg_replace or substr and . kept all the digits. For accurate timing, since floating-point arithmetic would lose precision, I stored microtime results as-is and calculated time difference with this function: <?php function microtime_used($before,$after) { return (substr($after,11)-substr($before,11)) +(substr($after,0,9)-substr($before,0,9)); } ?> For further information, the script itself, etc, see http://edorfaus.xepher.net/div/convert-method-test.php ---- Server IP: 66.163.161.117 Probable Submitter: 81.26.60.178 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.microtime.php Edit -- http://master.php.net/note/edit/66187 Del: integrated -- http://master.php.net/note/delete/66187/integrated Del: useless -- http://master.php.net/note/delete/66187/useless Del: bad code -- http://master.php.net/note/delete/66187/bad+code Del: spam -- http://master.php.net/note/delete/66187/spam Del: non-english -- http://master.php.net/note/delete/66187/non-english Del: in docs -- http://master.php.net/note/delete/66187/in+docs Del: other reasons-- http://master.php.net/note/delete/66187 Reject -- http://master.php.net/note/reject/66187 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#111405) next »