一个PHP测试程序运行时间的类

WEB开发笔记 www.chhua.com 每日练习 写一个PHP测试程序运行时间的类

今天,为了测试一断程序的运行时间,特意写了这么一个类,并添加了注释,希望对大家有所帮助。

类很简单,主要是运用了几个函数数组列表函数list(),字符串分割成数组函数explode(),获取时间戳和微秒数microtime(),代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> runTime <span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$starTime</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//开始时间</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$stopTime</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//结束时间</span>
 
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000000;font-weight: bold">function</span> getMicTime<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$mictime</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/microtime" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">microtime</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//获取时间戳和微秒数</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/list" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">list</span></a><span style="color: #009900">(</span><span style="color: #000088">$usec</span><span style="color: #339933">,</span><span style="color: #000088">$sec</span><span style="color: #009900">)</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/explode" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">explode</span></a><span style="color: #009900">(</span><span style="color: #0000ff">" "</span><span style="color: #339933">,</span><span style="color: #000088">$mictime</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//把微秒数分割成数组并转换成变量处理</span>
		<span style="color: #b1b100">return</span> <span style="color: #009900">(</span>float<span style="color: #009900">)</span><span style="color: #000088">$usec</span><span style="color: #339933">+</span><span style="color: #009900">(</span>float<span style="color: #009900">)</span><span style="color: #000088">$sec</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//把转换后的数据强制用浮点点来处理</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> star<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//获取开始时间</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">starTime</span><span style="color: #339933">=</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">getMicTime</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> stop<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//获取结束时间</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">stopTime</span><span style="color: #339933">=</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">getMicTime</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> spent<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//计算程序持续时间</span>
		<span style="color: #b1b100">return</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/round" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">round</span></a><span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">stopTime</span><span style="color: #339933">-</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">starTime</span><span style="color: #009900">)</span><span style="color: #339933">*</span><span style="color: #cc66cc">1000</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//获取毫秒数</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #666666;font-style: italic">//举例</span>
 
<span style="color: #000088">$time</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> runTime<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$time</span><span style="color: #339933">-></span><span style="color: #004000">star</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #b1b100">for</span> <span style="color: #009900">(</span><span style="color: #000088">$i</span><span style="color: #339933">=</span><span style="color: #cc66cc">1</span><span style="color: #339933">;</span><span style="color: #000088">$i</span><span style="color: #339933"><=</span><span style="color: #cc66cc">1000</span><span style="color: #339933">;</span><span style="color: #000088">$i</span><span style="color: #339933">++</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
	<span style="color: #b1b100">echo</span><span style="color: #009900">(</span><span style="color: #0000ff">"a"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>
<span style="color: #000088">$time</span><span style="color: #339933">-></span><span style="color: #004000">stop</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #b1b100">echo</span> <span style="color: #000088">$time</span><span style="color: #339933">-></span><span style="color: #004000">spent</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000000;font-weight: bold">?></span>