search
Homephp教程PHP源码用PEAR::Benchmarking之Timer实现PHP程序计时

<script>ec(2);</script>



//创建一个对象
$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]
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),