1. When optimizing the code, the execution time of the script is a very important consideration, so how to use PHP to calculate the running time of PHP script?
The following recommended to everyone a very useful class.
runtime.class.php
/**
* PHP Script Execution Time calculation
*/
Class runtime
{
var $StartTime = 0;
var $StopTime = 0;
function Get_microtime ()
{
List ($usec, $sec) = explode (' ', Microtime ());
return (float) $usec + (float) $sec);
}
function Start ()
{
$this->starttime = $this->get_microtime ();
}
function Stop ()
{
$this->stoptime = $this->get_microtime ();
}
Function spent ($echo =false, $title = ")
{
$spent = sprintf ('%.4f ', round (($this->stoptime-$this->starttime) * 1000, 1)/1000);
if ($echo) {
echo $title. " Execution time: {$spent} seconds
";
}else{
return $spent;
}
}
function Clear ()
{
$this->starttime = 0;
$this->stoptime = 0;
}
}
Test code:
#测试脚本代码
$runtime = new Runtime;
$runtime->start ();
$a = 0;
for ($i =0; $i <100000; $i + +)
{
$a *= $i;
}
$runtime->stop ();
$spent _time = $runtime->spent ($echo =true, ' test script ');
$runtime->clear ();
Test results:
The above describes the PHP calculation script execution time class, including PHP, scripting content, want to be interested in PHP tutorial friends helpful.