Home> php教程> PHP源码> body text

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

Mary-Kate Olsen
Release: 2016-06-08 17:31:37
Original
1002 people have browsed it



//创建一个对象
$timer = new Benchmark_Timer();

//计时开始
$timer->start();

//测试的一段脚本
for ($i=0; $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]
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!