Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.
<?php
$starttime = (float) array_sum( explode(' ', microtime()));
// --- Stuff that takes time ---
$i = 500000;
while ($i) $i--;
// --- end of stuff that takes time
$executiontime = (float) array_sum( explode(' ', microtime())) - $starttime;
// log or store or echo or whatever it is useful for..
echo 'Page created in ' . sprintf('%.3f',$executiontime) . ' secs.'."\n";
?>
Edit/Add: Using a class as posted later is even more waste of resources, if you want to meassure how long your page takes to load I assume you are converned with resource usage and code profiling, don't make it worse by adding extra cr"p that just slows down your profiling work..
Last edited by Stoker on Tue Mar 30, 2004 9:41 pm, edited 1 time in total.
/*
* Compute the time transcurred between two moments
*/
class Chronometer
{
var $startTime;
var $endTime;
function start()
{
$this->startTime=gettimeofday();
}
function stop()
{
$this->endTime=gettimeofday();
}
function show($precision=2)
{
$time = (float)($this->endTime['sec'] - $this->startTime['sec']) + ((float)($this->endTime['usec'] - $this->startTime['usec'])/1000000);
return (round($time, $precision));
}
}