[代码] [PHP]代码
01 | <?php |
02 | class runtime |
03 | { |
04 | var $StartTime = 0; |
05 | var $StopTime = 0; |
06 | |
07 | function get_microtime() |
08 | { |
09 | list($usec, $sec) = explode(' ', microtime()); |
10 | return ((float)$usec + (float)$sec); |
11 | } |
12 | |
13 | function start() |
14 | { |
15 | $this->StartTime = $this->get_microtime(); |
16 | } |
17 | |
18 | function stop() |
19 | { |
20 | $this->StopTime = $this->get_microtime(); |
21 | } |
22 | |
23 | function spent() |
24 | { |
25 | return round(($this->StopTime - $this->StartTime) * 1000, 1); |
26 | } |
27 | |
28 | } |
29 | |
30 | |
31 | //例子 |
32 | $runtime= new runtime; |
33 | $runtime->start(); |
34 | |
35 | //你的代码开始 |
36 | |
37 | $a = 0; |
38 | for($i=0; $i<1000000; $i++) |
39 | { |
40 | $a += $i; |
41 | } |
42 | |
43 | //你的代码结束 |
44 | |
45 | $runtime->stop(); |
46 | echo "页面执行时间: ".$runtime->spent()." 毫秒"; |
47 | ?> |
PHP运行时间测量
本文介绍了一个简单的PHP类,用于测量代码段的执行时间。通过使用microtime()函数获取精确的时间戳,该类能够准确地记录代码开始和结束的时间,并计算总的执行时间(以毫秒为单位)。示例代码展示了如何使用此类来评估一个具体任务的运行时间。
1304

被折叠的 条评论
为什么被折叠?



