note 75508 deleted from function.microtime by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 03:26:37 +0000 |
| Subject: | note 75508 deleted from function.microtime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142060@lists.php.net to get a copy of this message | ||
Note Submitter: alreece45 at yahoo dot com
----
I ran my own tests based on five of the functions here to emulate the PHP5 behavior on PHP4. As
always, these aren't needed on PHP5, but I was intrested in which one would run most quickly.
My results are as follows:
Function 1 (example): performs at 1x for these results
Function 2 (posted by yhoko): performs at about 1.033x
Function 3 (posted by james): performs at about 1.031x
Function 4 (posted by emuxperts admin/m0sh3) performs at about 0.945x
Function 5 (posted by Z0d): performs at about 1.103x
So if you're concerned about peformance, consider the use of the strtok() function as Z0d
used. Most of us, however, aren't going to need that sort of speed in a microtime function. In
a test of 1 million iterations, it saved about 1.3 seconds. I'm not sure of many php
applications that would use microtime that extensively. If so, how many of those microtimes is the
float value actually needed.
I ran this test many times in different orders (modify the $functions var) and many times.
Here is the output I used for these results:
Function [float_microtime_1] Total Time: 13.6581590175628662
Function [float_microtime_1] Average Time: 0.0000136581590176
Function [float_microtime_2] Total Time: 13.2114200592041016
Function [float_microtime_2] Average Time: 0.0000132114200592
Function [float_microtime_3] Total Time: 13.2385060787200928
Function [float_microtime_3] Average Time: 0.0000132385060787
Function [float_microtime_4] Total Time: 14.4395959377288836
Function [float_microtime_4] Average Time: 0.0000144395959377
Function [float_microtime_5] Total Time: 12.3734378814697266
Function [float_microtime_5] Average Time: 0.0000123734378815
Here's my test script:
<?php
function float_microtime_1() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function float_microtime_2() {
$time = microtime();
return (double)substr( $time, 11 ) + (double)substr( $time, 0, 8 );
}
function float_microtime_3() {
return array_sum(explode(' ',microtime()));
}
function float_microtime_4() {
return (float)preg_replace('#^0\.([0-9]+) ([0-9]+)$#', '\2.\1', microtime());
}
function float_microtime_5() {
return strtok(microtime(), ' ') + strtok('');
}
// settings for benchmark.
$functions = array(
'float_microtime_5',
'float_microtime_1',
'float_microtime_2',
'float_microtime_3',
'float_microtime_4',
);
$amount = 1000000;
set_time_limit(0);
// actual benchmark
$count = count($keys);
if($count > 0) {
// first get the functions in memory or w/e... sortof
foreach($functions as $function) {
$value = $function();
}
// run the test
foreach($functions as $function) {
$times = $amount;
$start[$function] = microtime();
while(--$times) {
$value = $function();
}
$stop[$function] = microtime();
}
foreach($functions as $function) {
$start_time = strtok($start[$function], ' ') + strtok('');
$stop_time = strtok($stop[$function], ' ') + strtok('');
$total_time = $stop_time - $start_time;
$average_time = $total_time / $amount;
echo 'Function [' . $function . '] Total Time: ' . number_format($total_time,
16) . chr(10);
echo 'Function [' . $function . '] Average Time: '.
number_format($average_time, 16) . chr(10);
}
}
else {
echo 'No Tests to run.';
}
?>