Home arrow PHP arrow Page 2 - Benchmarking Applications with PHP

Using the microtime() built-in PHP 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 introduction, one of the simplest ways to create a basic benchmarking mechanism relies on using the “microtime()” built-in PHP function. Just in case you didn’t know, this function returns the current Unix timestamp, which additionally appends the corresponding microseconds to the output. 

According to the definition from the PHP manual, the mentioned function returns the string "msec sec," where sec is the current time measured in the number of seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT), and "msec" is the microseconds part. Both portions of the string are returned in units of "seconds.” In simple terms, the syntax for the “microtime()” function is as follows:

mixed microtime ( [bool get_as_float] )

Additionally, when you’re using PHP 5, the function accepts the optional “get_as_float” parameter, which simply returns the complete timestamp as a float number.

As you may have guessed from the above definition, this function is quite handy for developing a simple benchmarking mechanism that only requires a few lines of code. Therefore, here is a basic custom function that uses “microtime().” Take a look at the definition of this function:

// define 'getMicrotime()'function in PHP 4

function getMicrotime(){

list($useg,$seg)=explode(' ',microtime());

return ((float)$useg+(float)$seg);


The “getMicrotime()” function that I defined above demonstrates a classic use of “microtime()” to construct a basic benchmarking system. It’s one of the most common implementations of the function that I ever saw.

That being said, let me go one step further and show you a simple example of how to use the previous “getMicrotime()” function inside of a benchmarking script. Here is the pertinent code sample:


// do nothing for a while




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

In this case, I simply applied my common sense to create a primitive PHP4-based benchmarking script. As you saw, the example first calls up the “getMicrotime()” function and stores the corresponding timestamp. Then it does nothing for a while. Finally, it displays the elapsed time via another function call. Very simple, isn’t it?

Of course, the previous example would be rather incomplete if I don’t show you the respective output generated by the above script, so here it is:

Time spent in doing nothing was 0.00837111473083 seconds

Okay, at this point, I created a fairly precise benchmarking function that can be used for timing different portions of code, or even complete applications. I do not mean that the prior example can be used freely in production environments, but it shows, in a nutshell, how easy it is to create these kind of scripts using only the “microtime()” function.

In the following section, I’ll show you how to use this helpful function with PHP 5. Thus, you can spot the difference between the different versions of the language.

To learn more on this topic, all you have to do is click on the link that appears below and read the next few lines. See you there.

>>> 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: