note 71629 added to function.microtime

From: Date: Fri, 08 Dec 2006 16:53:11 +0000
Subject: note 71629 added to function.microtime
Groups: php.notes 
Request: Send a blank email to php-notes+get-120439@lists.php.net to get a copy of this message
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. ---- Server IP: 194.153.94.10 Probable Submitter: 80.219.154.129 ---- Manual Page -- http://www.php.net/manual/en/function.microtime.php Edit -- https://master.php.net/note/edit/71629 Del: integrated -- https://master.php.net/note/delete/71629/integrated Del: useless -- https://master.php.net/note/delete/71629/useless Del: bad code -- https://master.php.net/note/delete/71629/bad+code Del: spam -- https://master.php.net/note/delete/71629/spam Del: non-english -- https://master.php.net/note/delete/71629/non-english Del: in docs -- https://master.php.net/note/delete/71629/in+docs Del: other reasons-- https://master.php.net/note/delete/71629 Reject -- https://master.php.net/note/reject/71629 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120439) next »