计算php代码执行时间长短的类(精确到毫秒)
2018-01-16 09:57
561 查看
<?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();
?>
相关文章推荐
- 计算php代码执行时间长短的类
- php计算某段代码执行时间
- C++精确计算代码执行时间精度微秒级
- C++精确计算代码执行时间精度微秒级
- php使用microtime(true)计算php程序代码执行消耗时间
- 获取C#代码执行的时间(精确到毫秒)
- VC精确计算代码执行时间
- 精确计算代码执行的时间差(毫秒)-QueryPerformanceFrequency。
- 解析PHP计算页面执行时间的实现代码
- 分享一个PHP代码的执行时间,精确到微秒级
- VC精确计算代码执行时间方法
- php 计算代码执行时间
- php 计算代码执行时间
- 精确计算代码执行时间
- JNI调用C计算代码执行时间(毫秒)
- 精确计算代码执行时间
- 获取C#代码执行的时间(精确到毫秒)
- c++怎么计算代码运行时间到超精确毫秒级别
- 精确计算代码执行时间
- php计算代码执行时间