首页 > php代码 > 用PEAR::Benchmarking之Timer实现PHP程序计时

用PEAR::Benchmarking之Timer实现PHP程序计时

//创建一个对象 

$timer = new Benchmark_Timer(); 


//计时开始 

$timer->start(); 


//测试的一段脚本 

for ($i=0; $i<1000; $i++) 

//we do nothing here 


//设置标记 

$timer->setMarker("Mark1"); 


//输出两点之间的用时 

echo $timer->timeElapsed("Start", "Mark1"); 

?> 

然后执行之,就可以得到0.000543这样的结果,大致为从开始到Mark1点之间的耗时.

类方法

方法:Benchmark_Timer( [mixed $auto = false]) 

描述:构造器,开始计时器记录.

参数:boolean $auto,缺省为false,如果设定为true的话,则会自动打印计时器结果.形如下表

    time index    ex time    %

Start    1099020859.80505200    -    0.00%

Stop    1099020859.80595000    0.000898    100.00%

total    -    0.000898    100.00%


分别列出开始/终止的时间索引,与上一标记之间的运行时间,与上一标记之间的运行时间占总耗时的百分比(所以Stop点一直为100%),以及总耗时. 

方法: display( ) 

描述:打印由getOutput方法返回的信息. 

形如下表:

    time index    ex time    %

Start    1099021292.32145600    -    0.00%

Mark1    1099021292.32202000    0.000564    46.38%

Mark2    1099021292.32209700    0.000077    6.33%

Stop    1099021292.32267200    0.000575    47.29%

total    -    0.001216    100.00%


列出每一标记的时间索引,与上一标记之间的运行时间,与上一标记之间的运行时间占总耗时的百分比,以及总耗时.

调用该方法前,应该先调用stop()来终止计时(并不会终止程序).见例2. 方法: getOutput( ) 

描述:返回格式化后的计时器信息. 该方法把计时器信息放如一个表中(如上表),供display()方法列出.

方法:getProfiling( ) 

描述:返回计时器信息.将是形如

Array 

[0] => Array 

[name] => Start 

[time] => 1099021787.69669100 

[diff] => - 

[total] => 1099021787.696691 


[1] => Array 

[name] => Mark1 

[time] => 1099021787.69675900 

[diff] => 0.000068 

[total] => 1099021787.696759 


[2] => Array 

[name] => Mark2 

[time] => 1099021787.69683500 

[diff] => 0.000076 

[total] => 1099021787.696835 


[3] => Array 

[name] => Stop 

[time] => 1099021787.69694500 

[diff] => 0.000110 

[total] => 1099021787.696945 


的 二维数组.0,1,2...为标记所在位置.假设x为某一标记所在位置,则$profiling[x]['name'] = 标记x的名称 $profiling[x]['time'] = 标记x的时间索引 $profiling[x]['diff'] = 从x-1标记到x标记之间运行的时间差 $profiling[x]['total'] = 在标记x以前所有的运行时间.

方法:setMarker( string $name) 

描述:设置标记.

参数:string $name - 欲设置的标记的名称,且区分大小写.


方法:start( ) 

描述:设置"开始(Start)"标记,也代表着计时的开始.

方法:stop( ) 

描述:设置"终止(Stop)"标记,也代表着计时的终止.


方法:double timeElapsed( [string $start = 'Start'], [string $end = 'Stop']) 

描述:返回两个标记之间的耗时.

参数:

string $start - 开始标记,缺省为"Start"

string $end - 结束标记,缺省为"Stop"

含6位小数.

应用

1,记录两点的时间差

//创建一个对象 

$timer = new Benchmark_Timer(); 


//设置标记 ("Mark1") 

$timer->setMarker("Mark1"); 


//一段测试 

for ($i=0; $i<100; $i++) 

//we do nothing here 


$timer->setMarker("Mark2"); 



//输出两点之间的用时 

echo $timer->timeElapsed("Mark1", "Mark2"); 

2,列出计时器的信息.

//创建一个对象 

$timer = new Benchmark_Timer(); 


//计时开始 

$timer->start(); 


$timer->setMarker("Mark1"); 


//一段测试 

for ($i=0; $i<100; $i++) 

//we do nothing here 


$timer->setMarker("Mark2"); 


//输出两点之间的用时 

echo $timer->timeElapsed("Mark1", "Mark2"); 


//中止计时 

$timer->stop(); 

$timer->display(); 

总结

显然如果你愿意,你可以在程序中设置无数个标记,如果你的一个程序运行速度较慢,你或许可以用它来测测到底哪段程序耗时最长,以近可能快速的找出问题所在.

 


本文链接:http://www.phprm.com/code/5424c4683579f05fb5898a59b048dc91.html

收藏随意^^请保留教程地址.

标签:none

发表留言