note 71629 deleted from function.microtime by tularis

From: Date: Sun, 22 Jun 2008 12:07:38 +0000
Subject: note 71629 deleted from function.microtime by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-140673@lists.php.net to get a copy of this message
Note Submitter: heavyraptor ---- By the way, I forgot to post my microtime_float() test results. Here's my script: <?php error_reporting(E_ALL); header('Content-type: text/plain'); function microtime_float1() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } function microtime_float2() { return array_sum(explode(' ',microtime())); } // Init for ($i = 0; $i < 10; $i++) microtime(); $ms1 = array(); $me1 = array(); $ms2 = array(); $me2 = array(); for ($i = 0; $i < 10000; $i++) { // microtime_float1() $ms1[] = microtime(); microtime_float1(); $me1[] = microtime(); for ($j = 0; $j < 4; $j++) microtime(); // microtime_float2() $ms2[] = microtime(); microtime_float2(); $me2[] = microtime(); } // Parse time foreach ($ms1 as $k => $time) $ms1[$k] = array_sum(explode(' ',$time)); foreach ($me1 as $k => $time) $me1[$k] = array_sum(explode(' ',$time)); foreach ($ms2 as $k => $time) $ms2[$k] = array_sum(explode(' ',$time)); foreach ($me2 as $k => $time) $me2[$k] = array_sum(explode(' ',$time)); // Calculate average $ms1 = array_sum($ms1) / count($ms1); $me1 = array_sum($me1) / count($me1); $ms2 = array_sum($ms2) / count($ms2); $me2 = array_sum($me2) / count($me2); echo 'microtime_float1() ' . number_format($me1 - $ms1,10) . "\n"; echo 'microtime_float2() ' . number_format($me2 - $ms2,10); ?> This script calculates the used time by microtime_float1() and microtime_float2(). I get the following results as the biggest differences: microtime_float1() 0.0000882149 microtime_float2() 0.0000278950 ... and these as lowest differences: microtime_float1() 0.0000467300 microtime_float2() 0.0000417233 of course this may change everytime you reexecute the script. The differences are very little, but this may be important in some scripts. Result: As you see, my microtime_float() function is better :D Anyway, this is all kind of nonsense, because most of the people use PHP 5 and there we just use microtime(true), which gives us the same result as my microtime_float(). So if you're using PHP < 5, use this function below: <?php function microtime_float() { return array_sum(explode(' ',microtime())); } ?> Thank you for your attention, have fun :). ... and sorry because of my bad english.

« previous php.notes (#140673) next »