timing php functions
Posted: Wed Dec 28, 2005 11:54 pm
I want to measure how long a certain section of code takes to complete, any ideas on how this is possible?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<?Php
$time_started = microtime();
sleep(100);
$time_ended = microtime();
$time_loaded = $time_ended - $time_started;
print $time_loaded; // will print someting like 0.2154854213
?>Code: Select all
function getmicrotime(){
$time = gettimeofday();
return $time['sec'].str_pad($time['usec'], 6, 0, STR_PAD_LEFT);
}Code: Select all
<?php
/**
* Simple function to replicate PHP 5 behaviour
*/
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
// Sleep for a while
usleep(100);
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Did nothing in $time seconds\n";
?>