PHP
  Home arrow PHP arrow Page 2 - Benchmarking Applications with PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 4
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT