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.

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:

   require_once 'Benchmark/Timer.php';
   function fibonacci(){
      //create an instance of the Benchmark_Timer class 
      $timer = new Benchmark_Timer();
      //Set "Start" marker
      for ($i = 0; $i < 10; $i++) {
         //Set the markers fibonacci
      //Set "Stop" marker
      //Returns formatted informations
      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
      echo '</pre>';
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.

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