Home arrow PHP arrow Page 3 - Benchmarking Applications with PHP

Simplifying the use of the microtime() function with PHP 5 - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the previous section, I mentioned that PHP 5 offered a slightly modified version of the native “microtime()” function, since there’s no need to create a custom function to obtain a float number from it. But you have to make sure to call this function with the “get_as_float” parameter. Then it will neatly return the corresponding timestamp as a float value.

In order to illustrate the usage of the “microtime()” function with PHP 5, I created a basic example that shows how to use this function for benchmarking purposes. Have a look a the following code sample:


// example using 'microtime()' function with PHP 5

$startTime=microtime(true);

// do nothing for a while

usleep(100);

$endTime=microtime(true);

$totalTime=$endTime-$startTime;

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


In this case, the above script simply calls the “microtime()” function with the “get_as_float” parameter set to true. Lastly, it displays the elapsed time as a float number. Of course, this condition is clearly demonstrated by the below output:

Time spent in doing nothing was 0.00532197952271 seconds

In addition to the example that you learned before, it’s possible to define a simple wrapper for the “microtime()” function in PHP 5 and create the following script:


// define 'getMicrotime()'function - PHP 5

function getMicrotime(){

return microtime(true);

}

$startTime=getMicrotime();

// do nothing for a while

usleep(100);

$endTime=getMicrotime();

$totalTime=$endTime-$startTime;

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

/*

displays the following:

Time spent in doing nothing was 0.00789403915405 seconds

*/


As you can see, creating simple and effective timing scripts is something that can easily be done with PHP 4 or PHP 5, and they can be used as decent benchmarking approaches too.

So far, I demonstrated the capacity offered by PHP 4 and PHP 5 respectively for developing simple and efficient timing functions. However, it’s possible (and even desirable) to go one step further and develop an object-oriented timing approach that can be used for benchmarking purposes as well.

In this case, I’m simply talking about creating a timer class that can be utilized, obviously, to time several scripts and applications together. Therefore, I suggest you read the following section, where I’ll explain how to build these specific classes.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap

Dev Shed Tutorial Topics: