| PHP PEAR Benchmarking Posted by: Jordan in Programming, PHP on Jun 26, 2008 |
In an earlier blog I told you that my benchmarking functions seemed flawed and that I would find a new one. I signed up for a PHP Unit Test class using Zend Studio but it isn't until the 19th so for now I've been playing with the PEAR Benchmark Package.
The class is easy to use:
The sample class in the documentation shows how simple it is to use.

Testing Echo
I wanted to test echo using single quotes and double quotes. The last tests I ran with my custom micro benchmarking function showed that double quotes were faster than single quotes which I believe is wrong. I rewrote the test using the PEAR Benchmark class.

The executefunction is a wrapper to execute any string passed to it as PHP code (using the eval function). You can see the different markers I set after executing a branch of code.
Results
And I get the results I expected:
I intend on doing more benchmarking with this class and once I complete the Zend Unit Testing webinar I'll blog about that as well. As always, if you need any help feel free to ask in our forum!
The class is easy to use:
- Set a "Start" marker
- Execute code you want to benchmark
- Set custom marker (your own marker)
- repeat 2/3 as many times as needed
- Set a "Stop" marker
The sample class in the documentation shows how simple it is to use.

- require_once 'Benchmark/Timer.php';
- function wait($amount) {
- for ($i=0; $i < $amount; $i ) {
- for ($i=0; $i < 100; $i );
- }
- }
- // Pass the param "true" to automatically display results
- $timer = new Benchmark_Timer();
- $timer->start();
- wait(10);
- $timer->setMarker('Mark1');
- echo "Elapsed time between Start and Mark1: " .
- $timer->timeElapsed('Start', 'Mark1') . "n";
- wait(50);
- $timer->stop();
- $timer->display();
require_once 'Benchmark/Timer.php';
function wait($amount) {
for ($i=0; $i < $amount; $i ) {
for ($i=0; $i < 100; $i );
}
}
// Pass the param "true" to automatically display results
$timer = new Benchmark_Timer();
$timer->start();
wait(10);
$timer->setMarker('Mark1');
echo "Elapsed time between Start and Mark1: " .
$timer->timeElapsed('Start', 'Mark1') . "n";
wait(50);
$timer->stop();
$timer->display();
function wait($amount) {
for ($i=0; $i < $amount; $i ) {
for ($i=0; $i < 100; $i );
}
}
// Pass the param "true" to automatically display results
$timer = new Benchmark_Timer();
$timer->start();
wait(10);
$timer->setMarker('Mark1');
echo "Elapsed time between Start and Mark1: " .
$timer->timeElapsed('Start', 'Mark1') . "n";
wait(50);
$timer->stop();
$timer->display();
Testing Echo
I wanted to test echo using single quotes and double quotes. The last tests I ran with my custom micro benchmarking function showed that double quotes were faster than single quotes which I believe is wrong. I rewrote the test using the PEAR Benchmark class.

- require 'Benchmark/Timer.php';
- function executeFunction($callback, $amount) {
- for ($i=0; $i < $amount; $i++) {
- eval($callback);
- }
- }
- // Pass the param "true" to constructor to automatically display the results
- $timer = new Benchmark_Timer();
- $timer->start();
- // Echo single
- executeFunction("echo 'CC';", 1000);
- $timer->setMarker('echo Single');
- $timer->timeElapsed('Start', 'echo Single') . "n";
- // Echo Double
- executeFunction("echo "CC";", 1000);
- $timer->setMarker('echo Double');
- $timer->timeElapsed('echo Single', 'echo Double') . "n";
- // Stop the microtimer and
- // Display the Results
- $timer->stop();
- $timer->display();
require 'Benchmark/Timer.php';
function executeFunction($callback, $amount) {
for ($i=0; $i < $amount; $i++) {
eval($callback);
}
}
// Pass the param "true" to constructor to automatically display the results
$timer = new Benchmark_Timer();
$timer->start();
// Echo single
executeFunction("echo 'CC';", 1000);
$timer->setMarker('echo Single');
$timer->timeElapsed('Start', 'echo Single') . "n";
// Echo Double
executeFunction("echo "CC";", 1000);
$timer->setMarker('echo Double');
$timer->timeElapsed('echo Single', 'echo Double') . "n";
// Stop the microtimer and
// Display the Results
$timer->stop();
$timer->display();
function executeFunction($callback, $amount) {
for ($i=0; $i < $amount; $i++) {
eval($callback);
}
}
// Pass the param "true" to constructor to automatically display the results
$timer = new Benchmark_Timer();
$timer->start();
// Echo single
executeFunction("echo 'CC';", 1000);
$timer->setMarker('echo Single');
$timer->timeElapsed('Start', 'echo Single') . "n";
// Echo Double
executeFunction("echo "CC";", 1000);
$timer->setMarker('echo Double');
$timer->timeElapsed('echo Single', 'echo Double') . "n";
// Stop the microtimer and
// Display the Results
$timer->stop();
$timer->display();
The executefunction is a wrapper to execute any string passed to it as PHP code (using the eval function). You can see the different markers I set after executing a branch of code.
Results
And I get the results I expected:
| time index | ex time | % | |
| Start | 1214519925.13337300 | - | 0.00% |
| echo Single | 1214519925.14421700 | 0.010844 | 48.17% |
| echo Double | 1214519925.15566500 | 0.011448 | 50.86% |
| Stop | 1214519925.15588300 | 0.000218 | 0.97% |
| total | - | 0.022510 | 100.00% |
I intend on doing more benchmarking with this class and once I complete the Zend Unit Testing webinar I'll blog about that as well. As always, if you need any help feel free to ask in our forum!