note 33448 deleted from function.microtime by didou
| From: | didou@php.net | Date: | Mon, 29 Dec 2003 15:20:59 +0000 |
| Subject: | note 33448 deleted from function.microtime by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-62640@lists.php.net to get a copy of this message | ||
Note Submitter: kenny
----
using the microtime function is possibly one of the worst ways to calculate execution time. I give
two examples, of which are much more precise.
1. gettimeofday -- calculate time passed using the sec and usec var of gettimeofday()
2. unix 'date' command -- calculate time passed using the nanosecond and second variables.
-- code example --
$start1=gettimeofday();
list($start2, $start3) = explode(":", exec('date +%N:%S'));
// insert your script here
$end1=gettimeofday();
$totaltime1 = (float)($end1['sec'] - $start1['sec']) +
((float)($end1['usec'] - $start1['usec'])/1000000);
list($end2, $end3) = explode(":", exec('date +%N:%S'));
$totaltime2 = (float)($end3 - $start3) + ((float)($end2 - $start2)/1000000000);
echo "$totaltime2\n";
echo "$totaltime1\n";
-- end code example --
You'd be surprised at how accurate these functions are, and if you don't believe me, run
php on the commandline using the time command 'time php script.php' -- it will print to
stdout, but you'll see script execution time.
peace,
kenny