PHP
  Home arrow PHP arrow Page 2 - Benchmarking Applications with PHP
The Best Selling PC Migration Utility.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Benchmarking Applications with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-04-23

    Table of Contents:
  • Benchmarking Applications with PHP
  • Using the microtime() built-in PHP function
  • Simplifying the use of the microtime() function
  • Using an object-oriented approach for benchmarking scripts
  • Defining a wrapper method for the PHP 5 microtime() function

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Benchmarking Applications with PHP - Using the microtime() built-in PHP function
    (Page 2 of 5 )

    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:


    $startTime=getMicrotime();

    // do nothing for a while

    usleep(100);

    $endTime=getMicrotime();

    $totalTime=$endTime-$startTime;

    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


       · One exciting thing that you want to do with PHP, is benchmarking a particular...
       · The technique described (using microtime()) has limitations in that you must add the...
       · Thank you for commenting on my PHP article and for mentioning the xdebug extension....
     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway