PHP 计算页面执行时间
2009-05-12 17:13
543 查看
经常看到很多网站的页面底部有个"页面执行时间"的冬冬,如果你是程序员的话,还能够用它调试自己的程序执行效率,其实原理很简单,相信你看过本文后,很容易就能实现.
PHP:
1 <?php
2 // 说明:PHP 计算页面执行时间
3 // 整理:labs.cnblogs.com
4
5 class timer
6 {
7 var $StartTime = 0;
8 var $StopTime = 0;
9
10 private function get_microtime()
11 {
12 list($usec, $sec) = explode(' ', microtime());
13 return ((float)$usec + (float)$sec);
14 }
15
16 public function start()
17 {
18 $this->StartTime = $this->get_microtime();
19 }
20
21 public function stop()
22 {
23 $this->StopTime = $this->get_microtime();
24 }
25
26 public function spent()
27 {
28 return round(($this->StopTime - $this->StartTime) * 1000, 1);
29 }
30
31 }
32
33 /*
34 //例子
35 $timer = new timer;
36 $timer->start();
37
38 //你的代码开始
39
40 $a = 0;
41 for($i=0; $i<1000000; $i++)
42 {
43 $a += $i;
44 }
45
46 //你的代码结束
47
48 $timer->stop();
49 echo "页面执行时间: ".$timer->spent()." 毫秒";
50 */
51 ?>
PHP:
1 <?php
2 // 说明:PHP 计算页面执行时间
3 // 整理:labs.cnblogs.com
4
5 class timer
6 {
7 var $StartTime = 0;
8 var $StopTime = 0;
9
10 private function get_microtime()
11 {
12 list($usec, $sec) = explode(' ', microtime());
13 return ((float)$usec + (float)$sec);
14 }
15
16 public function start()
17 {
18 $this->StartTime = $this->get_microtime();
19 }
20
21 public function stop()
22 {
23 $this->StopTime = $this->get_microtime();
24 }
25
26 public function spent()
27 {
28 return round(($this->StopTime - $this->StartTime) * 1000, 1);
29 }
30
31 }
32
33 /*
34 //例子
35 $timer = new timer;
36 $timer->start();
37
38 //你的代码开始
39
40 $a = 0;
41 for($i=0; $i<1000000; $i++)
42 {
43 $a += $i;
44 }
45
46 //你的代码结束
47
48 $timer->stop();
49 echo "页面执行时间: ".$timer->spent()." 毫秒";
50 */
51 ?>