Miscellaneous Code

  Home arrow Miscellaneous Code arrow Quick 'n Easy Benchmark
MISCELLANEOUS CODE

Quick 'n Easy Benchmark
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-12-05

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    This is a really easier little function that you can use as a way to test how fast something executes. I use this for the PHP contests.

    By : jinxidoru

    <?php
    /*
    * Function: benchmark
    * Author: Michael Bailey <mpbailey@byu.edu>
    * Date: 5 Dec 2002
    * License: GPL (General Public License)
    * Purpose: This function will run the line of code
    * sent as the first parameter and return
    * the time it took to complete execution.
    * If a second parameter is passed, the
    * function will run the code the specified
    * number of times, taking the average. This
    * will return a better approximation. The
    * last parameter allows you to send code to
    * be executed between iterations, in case
    * something needs to be reset (ie. global
    * variables, etc...). The function returns
    * the number of seconds
    * with a bunch of decimal places.
    */
    function benchmark($code, $iter = 1, $reset_code = null)
    {
    # Determine the overhead in calling the eval function
    # You might want to to this multiple times to get better
    # precision.
    $start = microtime();
    eval("");
    $end = microtime();
    list($start_ms, $start_s) = explode(" ", $start);
    list($end_ms, $end_s) = explode(" ", $end);
    $overhead = ($end_ms+$end_s) - ($start_ms+$start_s);

    # Execute the code the specified number of times
    for ($i=0; $i<$iter; $i++) {
    $start = microtime();
    eval($code);
    $end = microtime();
    list($start_ms, $start_s) = explode(" ", $start);
    list($end_ms, $end_s) = explode(" ", $end);
    $time += ($end_ms+$end_s) - ($start_ms+$start_s);

    # If reset code was specified, evaluate it
    if ($reset_code != null) eval($reset_code);
    }

    # Return the average speed subtracted by
    # the overhead of calling eval()
    return ($time/$iter) - $overhead;
    }
    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


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