作者roga (任性)
看板PHP
标题Re: [请益] PHP 执行时间的 class 或 function
时间Fri Jul 29 12:13:58 2011
※ 引述《tkdmaf (皮皮快跑)》之铭言:
: ※ 引述《chan15 (ChaN)》之铭言:
: : 请教一下,有没有哪边有不错的测试程式码执行时间软体
: : 一个页面「多段」测试,显示多段结果的
^^^^
请用 pear 的 benchmark pkg
http://pear.php.net/package/Benchmark/
require('/usr/share/php/Benchmark/Timer.php');
$timer = new Benchmark_Timer();
$timer->start();
$timer->setMarker('MethodStart');
/* 写想测试的方法 */
$timer->setMarker('MethodStop');
$timer->setMarker('FunctionStart');
/* 写想测试时间的函式 */
$timer->setMarker('FunctionStop');
$timer->timeElapsed('MethodStart', 'MethodStop');
$timer->timeElapsed('FunctionStart', 'FunctionStop');
/* 以此类推,可以自己多设一些 Marker 下去测试 */
$timer->stop();
$timer->display();
这样会有你要的测试结果。
另外如果要细一点的执行效能,
也可以考虑安装 xdebug 或是 xhprof (pecl 内都有)。
: 自己随手写一个:
: <?php
: class effect_time {
: var $start_time;
: var $end_time;
: function start(){
: $this->start_time = $this->_return_time();
: return $this->start_time.'<br>';
: }
: function end(){
: $this->end_time = $this->_return_time();
: return $this->end_time.'<br>';
: }
: function result(){
: echo $this->end_time - $this->start_time;
: }
: function _return_time(){
: list($sec,$min) = explode(' ',microtime());
: return $min+$sec;
: }
: }
: ?>
: 载入之後:
: $effect_time = new $effect_time;
: 在要测试的程式起始处:
: $effect_time->start();
: 终止处:
: $effect_time->end();
: 若要看起始或终始时间前面直接加echo。
: 若要在最後看,就把$effect_time->start_time或是end_time直接印出来。
: 效能时间:$effect_time->result();
: 大致上是这样。
建议使用 pear 的套件,功能比较全面。
--
The Internet: where men are men, women are men, and children are FBI agents.
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 202.89.121.16
※ 编辑: roga 来自: 202.89.121.16 (07/29 14:08)