PHP Timing Class
Posted: Mon Oct 09, 2006 2:28 am
Nothing fancy here...
The Code
Usage
Output
The Code
Code: Select all
<?php
/*
* This class will calculate time in seconds for
* timing script or function execution times
* Author - scottayy@gmail.com
*/
class timer
{
var $time_start;
var $time_end;
var $round_to = 5;
function start_timer()
{
$this->time_start = $this->microtime_float();
}
function stop_timer()
{
$this->time_end = $this->microtime_float();
}
function calculate_time()
{
return round($this->time_end - $this->time_start, $this->round_to);
}
function microtime_float()
{
if(version_compare('5.0.0',phpversion()))
{
return microtime(1);
} else
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
}
}
?>Code: Select all
$timer = new timer();
$timer->start_timer();
for($i=0;$i<1000000;$i++)
{
//do nothing.. or something?
}
$timer->stop_timer();
echo $timer->calculate_time();Code: Select all
0.11116