您的位置:首页 > 编程语言 > PHP开发

PHP计算运行时间

2011-08-27 23:11 211 查看
View Code

PHP计算程序运行时间的类

1 class timer {
2  var $StartTime = 0;
3 var $StopTime = 0;
4 var $TimeSpent = 0;
5 function start(){
6   $this->StartTime = microtime();
7 }
8 function stop(){
9   $this->StopTime = microtime();
10 }
11 function spent() {
12   if ($this->TimeSpent) {
13    return $this->TimeSpent;
14   } else {
15    $StartMicro = substr($this->StartTime,0,10);
16    $StartSecond = substr($this->StartTime,11,10);
17    $StopMicro = substr($this->StopTime,0,10);
18    $StopSecond = substr($this->StopTime,11,10);
19    $start = doubleval($StartMicro) + $StartSecond;
20    $stop = doubleval($StopMicro) + $StopSecond;
21    $this->TimeSpent = $stop - $start;
22    return substr($this->TimeSpent,0,8)."秒";
23   }
24 }
25 }//end class timer;
26
27
28 $timer = new timer;
29 $timer->start();
30 $temp=0;
31 for($i=0;$i<10000;$i++) for($j=0;$j<$i;$j++) $temp ++;
32 $timer->stop();
33 echo "循环 $temp 次,运行时间为 ".$timer->spent();

1 //脚本运行时间
2  $start_time = array_sum(explode(" ",microtime()));
3  $run_time = array_sum(explode(" ",microtime())) - $start_time;
4  echo('<center>Script Run Time: '.$run_time.'</center>');


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: