計算php程式碼執行時間長短的類(精確到毫秒)
阿新 • • 發佈:2019-01-25
<?php
/**
* PHP指令碼執行時間計算
*/
class runtime
{
var $StartTime = 0;
var $StopTime = 0;
function get_microtime()
{
list($usec, $sec) = explode(' ', microtime());
//var_dump($usec);var_dump($sec);
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));
//毫秒
//$msec = $spent*1000;
if($echo){
echo $title."執行時間:{$spent}毫秒<br/>";
}else{
return $spent;
}
}
function clear()
{
$this->StartTime = 0;
$this->StopTime = 0;
}
}
#測試指令碼程式碼
$runtime= new runtime;
$runtime->start();
$a = 0;
for($i=0; $i<100000; $i++)
{
$a *= $i;
}
$runtime->stop();
$spent_time = $runtime->spent($echo=true, '測試指令碼');
$runtime->clear();
?>
/**
* PHP指令碼執行時間計算
*/
class runtime
{
var $StartTime = 0;
var $StopTime = 0;
function get_microtime()
{
list($usec, $sec) = explode(' ', microtime());
//var_dump($usec);var_dump($sec);
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));
//毫秒
//$msec = $spent*1000;
if($echo){
echo $title."執行時間:{$spent}毫秒<br/>";
}else{
return $spent;
}
}
function clear()
{
$this->StartTime = 0;
$this->StopTime = 0;
}
}
#測試指令碼程式碼
$runtime= new runtime;
$runtime->start();
$a = 0;
for($i=0; $i<100000; $i++)
{
$a *= $i;
}
$runtime->stop();
$spent_time = $runtime->spent($echo=true, '測試指令碼');
$runtime->clear();
?>