使用microtime函数计算页面执行时间
1 < ?php 2 class runtime 3 { 4 var $StartTime = 0; 5 var $StopTime = 0; 6 7 function get_microtime() 8 { 9 list($usec, $sec) = explode(' ', microtime()); //explode(); 10 11 /* microtime();out:0.03125400 1259580902 如果调用时不带可选参数,本函数以 "msec sec" 的格式返回一个字符串,其中 sec 是自 Unix 纪元(0:00:00 January 1, 1970 GMT)起到现在的秒数,msec 是微秒部分。字符串的两部分都是以秒为单位返回的。 */ 13 14 return ((float)$usec + (float)$sec); 15 } 16 17 function start() 18 { 19 $this->StartTime = $this->get_microtime(); 20 } 21 22 function stop() 23 { 24 $this->StopTime = $this->get_microtime(); 25 } 26 27 function spent() 28 { 29 return round(($this->StopTime - $this->StartTime) * 1000, 1); 30 } 31 32 } 33 34 35 //例子 36 $runtime= new runtime; 37 $runtime->start(); 38 39 //你的代码开始 40 41 $a = 0; 42 for($i=0; $i<1000000; $i++) 43 { 44 $a += $i; 45 } 46 47 //你的代码结束 48 49 $runtime->stop(); 50 echo "页面执行时间: ".$runtime->spent()." 毫秒"; 51 52 ?>
Content form: http://sjolzy.cn/Page-execution-time-calculation.html