Home arrow PHP arrow Page 5 - Benchmarking Applications with PHP

Defining a wrapper method for the PHP 5 microtime() function - PHP

Benchmarking a particular application across its different development cycles is one of the most exciting things for a PHP developer. If you’re anything like me, you have had your head spinning with questions, such as… is the performance of my last project good enough? How long does it take to fetch a particular data set from my database? Do I have to implement a caching system? As you can see, these and other dilemmas (add your own to the list) sometimes make peace of mind a nearly impossible goal.

  1. Benchmarking Applications with PHP
  2. Using the microtime() built-in PHP function
  3. Simplifying the use of the microtime() function
  4. Using an object-oriented approach for benchmarking scripts
  5. Defining a wrapper method for the PHP 5 microtime() function
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
April 23, 2008

print this article



As I expressed in the section that you just read, it’s possible to define yet another timer class using the advantages offered by PHP 5 with reference to its “microtime()” function. This brand new timing class can be created as follows:

// define 'Timer' class with PHP 5

class Timer{

private $elapsedTime;

// start timer

public function start(){


throw new Exception('Error obtaining start time!');



// stop timer

public function stop(){


throw new Exception('Error obtaining stop time!');


return $this->elapsedTime;


//define private 'getMicrotime()' method

private function getMicrotime(){

return microtime();



In this case, the above “Timer” class looks nearly identical to its PHP4-based counterpart. Aside from using exceptions to handle potential errors, the class in question uses a simple wrapper for the native “microtime()” function and it can be put to work as follows:


// instantiate 'Timer' class

$timer=new Timer();

// start timer


// do nothing for a while


// stop timer


// display elapsed time

echo 'Time spent in doing nothing was '.$elapsedTime.' seconds';


catch(Exception $e){

echo $e->getMessage();



The previous example is very easy to grasp. Thus, it doesn’t merit any additional discussion here. However, feel free to tweak the source code of all the classes that I showed and add your own modifications to it. You’ll have a good time!

To wrap up

In this first article of the series, I introduced the basics on how to create benchmarking scripts with PHP 4 and PHP 5. This ranged from procedural approaches to object-oriented methods. You saw that there are different options for timing your applications.

However, this journey isn’t finished yet, since in the next tutorial, I’ll show you how to use some of these classes to evaluate the performance of different PHP scripts. Don’t miss the next article!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: