计算php代码执行时间长短的类(精确到毫秒)
生活随笔
收集整理的這篇文章主要介紹了
计算php代码执行时间长短的类(精确到毫秒)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?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代码执行时间长短的类(精确到毫秒)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 海南省中招网考生报名(2021海南满分考
- 下一篇: win10qq无法远程协助(win10q