Using Benchmark

0
Your rating: None

As the name implies, this is a simple class for benchmarking code. Benchmarking, in case you’re not familiar, is the process of timing how long code blocks, entire scripts, or whole applications take to run. Doing so can be a useful tool for finding bottlenecks in your sites and improving performance.

I’ll say up front that the Benchmark class is nice and simple, but the documentation for it is lacking. There is simply no end-user documentation available.

Benchmarking is a good way to give you an understanding of execution tendencies:
is it generally better to do this or that? When it comes time to really fine-tune your applications, you should benchmark your exact code (i.e., what you’ve written versus alternative methods) on the server it will run on. Only then will you know for certain that you’ve got the best performance
possible.

It should also be remembered that the benchmarking itself affects a script’s performance. Because of the extra memory required by the Benchmark object, and
the extra processing required by calling its methods, any benchmarked script will perform worse than its unbenchmarked version (not that there’s a way to test for that, of course).

Benchmarking some code

To simply benchmark some sections of code in a PHP script, use the Benchmark Timer class. Start by including the file and creating the object:

require (‘Benchmark/Timer.php’);
$timer = new Benchmark_Timer();
\

Once you have a timer object, you can start the timer at any point:

$timer->start();

Stopping the timer is a matter of:

$timer->stop();

Commonly, you’ll want to see how long specific parts of your code take to run. You can set place markers using:

$timer->setMarker(‘Marker Name’);

When you’re done doing whatever, you can easily report on the results by invoking the display() method:

$timer->display();

Or you can manually access the intervals between points using:
$timer->timeElapsed(‘Marker1’,‘Marker2’)

A final option is to get an array of results via:

$profile = $timer->getProfiling();

This array can then be used however you see fit.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.

Cumulus Tag Cloud

Kiran Says

I love work environment which is:

Informal yet professional
Demanding yet rewarding
Challenging yet inspiring
Mediocrity is not an option
Having fun is serious business
Making mistakes is human
Forgiveness is Company Policy

Travelling Sucks