PHP
  Home arrow PHP arrow Page 4 - 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? 
Google.com  
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 an object-oriented approach for benchmarking scripts
    ( Page 4 of 5 )

    If creating procedural timing scripts was a no-brainer process, then you’ll probably find that building a timer class is even simpler. Basically, constructing such a class depends on defining a couple of methods that behave as basic wrappers for the built-in PHP “microtime()” function.

    Considering the concepts deployed before, a typical timer class could be defined as follows:


    // define 'Timer' class with PHP 4

    class Timer{

    var $elapsedTime;

    // start timer

    function start(){

    if(!$this->elapsedTime=$this->getMicrotime()){

    trigger_error('Error obtaining start time!',E_USER_ERROR);

    }

    }

    // stop timer

    function stop(){

    if(!$this->elapsedTime=round($this->getMicrotime()-$this->elapsedTime,5)){

    trigger_error('Error obtaining stop time!',E_USER_ERROR);

    }

    return $this->elapsedTime;

    }

    //define private 'getMicrotime()' method

    function getMicrotime(){

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

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

    }

    }

    }


    As you can see, defining a timer class with PHP 4 is a very simple process. As I said before, all that I did here was create a wrapping method for the “microtime()” PHP native function, as well as define some additional methods that are useful for starting and stopping the corresponding timer.

    Of course, this is only an introductory example and can be greatly improved. However, if you need a basic but effective timer class and don't want to write complex code, you have one here that may fit your needs.

    All right, after showing you how to create a simple timer class with PHP 4, have a look at the following example, which demonstrates how to use it:


    // instantiate 'Timer' class

    $timer=&new Timer();

    // start timer

    $timer->start();

    // do nothing for a while

    usleep(100);

    // stop timer

    $elapsedTime=$timer->stop();

    // display elapsed time

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

    /*

    displays the following

    Time spent in doing nothing was 0.00996 seconds

    */


    As shown above, the entire process for timing or benchmarking a particular script is reduced to calling up the respective “start()” and “stop()” methods that belong to the “Timer” class. That’s it.

    At this point, you hopefully have a clear idea of how to develop a timer class with PHP 4, since this is a no-brainer procedure that can be tackled even by beginners. Therefore, the next lesson of this tutorial will be focused on creating another handy timer class, this time using PHP 5.

    Want to learn how this will be done? Jump into the following section and keep reading.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek