PHP Benchmark/Timer Class

by 清泉 17. 六月 2010 21:39

Overview

This is a PHP class that calculates the time between specified points. It can be used to determine how long your page took to generate or how long certain loops take to run. This class is very flexible because it allows you to specify any number of “marker” points in the code and then compare any of them together.

There is nothing fancy about it. I ran across a post from someone looking for something like this and claimed it was hard to find… so I figured I’d release what we have in an effort to help anyone else out there.
 

download

class.benchmark.zip (610.00 bytes)

Current Version: 1.0

Source is hosted at Google Code

Features

  • PHP5+ only
  • Sets the first marker automatically
  • Multiple markers
  • Markers are stored so they can also be used as a totalizer

How To Use It

The use is really basic. Which is good, because let’s be honest.. it’s just a timer.

A basic use:
 
1 $bm = new Benchmark();
2 //code here
3 $bm->mark('end');
4 echo $bm->elapsed('start', 'end') . " Seconds";
5 //note that 'start' was auto generated
Another use where we use other markers for something like sending an email
 
 
1 $bm = new Benchmark();
2   
3 $bm->mark('email_start');
4 //email code here
5 $bm->mark('email_stop');
6   
7 echo $bm->elapsed('email_start', 'email_stop') . " Seconds";
Using it as a totalizer
 
01 $bm = new Benchmark();
02   
03 $total_time = 0;
04 foreach($emails as $email){
05     $bm->mark('email_start');
06     //email code here
07     $bm->mark('email_stop');
08   
09     $total_time += $bm->elapsed('email_start', 'email_stop');
10 }
11   
12 echo $total_time . " Seconds";

Full Documentation

Initializing the Class

$bm = new Benchmark();

  • When the class is initialized a marker is auto-generated called ’start’ through the __construct method.

Public Methods

  • $this->mark(string $name)
    • Sets a marker with the name that is passed in.
    • $name – (string) Name of the marker you are setting

 

  • $this->elapsed(string $point1, string $point2 [, int $decimals = 4])
    • Gets the elapsed time between two marker points
    • $point1 – (string) Name of the first marker you are checking
    • $point2 – (string) Name of the second marker you are checking
    • $decimal – (integer, optional) Number of decimal places you want displayed in the result

Tags:

PHP技术资料

添加评论



(将显示你的Gravatar头像)  

biuquote
微笑得意调皮害羞酷大笑惊讶发呆喜欢可怜尴尬闭嘴噘嘴皱眉伤心抓狂呕吐坏笑漫骂发怒
Loading



Supidea.com 晨飞的梦 @ All Rights Reserved. Powered by BlogYi.NET ver:1.8.0.0. 苏ICP备09011404号

关于博主

kamau
抱着美好的理想背井离乡,这酸甜苦辣只能默默忍受。既然选择了路,就得风雨兼程……

Calendar

<<  二月 2012  >>
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

在日历中查看文章

最近的评论

Comment RSS

声明

      本博所发一切破解相关附件只作学习研究交流之用,严禁用于商业用途,请在下载24小时内删除。
      本博所有网友评论不代表本博立场,版权归其作者所有。

© Copyright 2009