Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Sharpen Your Code with Benchmarking in PHP

Discover how to optimize your PHP applications using these straightforward yet powerful benchmarking techniques.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

ny software product must successfully pass the optimization step before it can hit the market and become a reference product. Finding memory leaks and increasing a product's performance is a delicate job that takes many hours of work and many human resources. Benchmarking is an important step in the optimization puzzle, because it can test both individual chunks of code and the entire codebase, and provides reports and statistics that reveal the true runtime parameters and performance.

In PHP you can use the Benchmark package—a PEAR package used to benchmark PHP scripts or functions calls. The latest released version is 1.2.7 (stable); after downloading the package, you can install it like this:

>> pear install Benchmark-1.2.7

To illustrate the Benchmark package's capabilities you'll see two different solutions (iterative and recursive) for the classical problem of generating Fibonacci numbers. The complete problem description is beyond the scope of this article, but it's a very common problem. If you are not familiar with Fibonacci generation, or you just want to re-familiarize yourself with the Fibonacci sequence, you can find more information here.

The Class Trees for Benchmark PEAR
The package has a small set of flexible classes that you use to perform benchmark timing:

  • Benchmark_Timer
  • Benchmark_Iterate
  • Benchmark_Profiler
This article discusses each class, shows its commonly-used methods, and provides an example of using it.

The Benchmark_Timer Class
The Benchmark_Timer class provides a set of methods that return highly-accurate timing information. The prototypes for the most used methods of this class are:

  • void start(): Set the start time of the marker.
  • void stop(): Set the stop time of the marker.
  • void setMarker(string $name): Sets a marker. The $name parameter represents the name of the marker to set.
  • void display([boolean $showTotal = FALSE], [string $format = 'auto']): This function returns formatted information. If is set to true, the $showTotal parameter outputs verbose information. The $format parameter controls the desired output format (auto (the default), plain, or html). When the value is auto the PEAR implementation chooses between plain and html; in most cases it chooses plain.
  • array getProfiling(): Returns the profiler info as an associative array. The $profiling[x]['name'] value represents the name of marker x; the $profiling[x]['time'] value represents the time index of marker x; the $profiling[x]['diff'] value represents the execution time from marker x-1 to marker x; and the $profiling[x]['total'] value represents the total execution time up to marker x.
Benchmarking Fibonacci (Iterative Solution)
This example applies the Benchmark_Timer class to the iterative solution for the Fibonacci numbers, providing formatted timing information:

<?php require_once 'Benchmark/Timer.php'; function fibonacci(){ //create an instance of the Benchmark_Timer class $timer = new Benchmark_Timer(); //Set "Start" marker $timer->start(); $a=0;$b=1; for ($i = 0; $i < 10; $i++) { $s=$a+$b; //Set the markers fibonacci $timer->setMarker('fibonacci'.$i); $a=$b; $b=$s; } //Set "Stop" marker $timer->stop(); //Returns formatted informations $timer->display(); echo '<pre>'; //Get the profiler info as an associative array $profiling = $timer->getProfiling(); //Display all the information: name, // time, difference between two //consecutive markers and total time print_r($profiling[1]); print_r($profiling[2]); print_r($profiling[3]); echo '</pre>'; } fibonacci(); ?>

The code sets a start marker, and then begins a loop that generates the first 10 numbers in the Fibonacci series. After each generation, it sets a marker named "fibonacci" plus the loop counter value (fibonacci1, fibonacci2, etc.).

Listing 1 contains similar code that benchmarks the recursive Fibonacci solution.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date