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
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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