Date Time Code

  Home arrow Date Time Code arrow Benchmarker

By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1

    Table of Contents:



    This benchmark script checks the speed of each method in any classes it is given. It is a very effective way to compare two different algorithms.

    By : jinxidoru

    * This script runs every method in a set of classes a specified
    * number of times. The total time taken as well as the average
    * time per iteration is printed after each test.
    * The script is run from the command line with as many arguments
    * as desired. An attempt is made to require each file given on
    * the command-line. The last element of the arg list, specifies
    * the number of iterations to execute for each method. For
    * example, the following call loads array_funcs.php and iterates
    * 100 times for each method:
    * $ php -Cq benchmark.php array_funcs.php 100
    * After loading all of the specified files, any non system class
    * is instantiated and each of its methods are executed a number
    * of times equal to the iteration value. When complete, a line
    * containing the method, total execution time (ms), and average
    * execution time per call (ms) is displayed.
    * Author: Michael Bailey (
    * Date: June 2004
    * License: GPL

    // get an array of the system classes
    $system_classes = get_declared_classes();

    // load the class and file
    foreach ($_SERVER['argv'] AS $i => $file)
    @include_once $file;

    // get the iteration count
    $iter = $_SERVER['argv'][count($_SERVER['argv'])-1];
    if ($iter < 1) $iter = 1;

    // benchmark every class
    foreach (array_diff(get_declared_classes(),$system_classes) AS $class) {
    print "\n-= $class =-\n";

    // create an instance
    $inst = new $class();

    // test each method
    foreach (get_class_methods($class) AS $method) {
    // get start time
    $start = microtime();

    // run method
    for ($i=0; $i<$iter; $i++) $inst->$method();

    // parse time
    $stop = microtime();
    list($start_usec, $start_sec) = explode(' ',$start);
    $start = ((float)$start_usec + (float)$start_sec);
    list($stop_usec, $stop_sec) = explode(' ',$stop);
    $stop = ((float)$stop_usec + (float)$stop_sec);
    $elapsed = ($stop-$start)*1000;

    // display elapsed time
    printf(" @ (%d) %15s: %4.3fms ~ %4.3fms\n", $iter, $method, $elapsed, $elapsed/$iter);

    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 Date Time Code Articles
    More By Codewalkers

    blog comments powered by Disqus


    - DaysInSpan.php
    - MySQLdateSpan.php
    - DateSpan.php
    - FutureDateFormEntry.php
    - Generate Time Option List for Select stateme...
    - Current Age Script (v2)
    - class for some mysql imitated time functions
    - Current Age Script, up to the last day
    - filemtime_remote
    - Page Generation Time Figure-Outer
    - convGMT v2
    - Benchmarker
    - Simple PHP Calendar
    - Display message according to hour of day
    - Display Date Function

    Developer Shed Affiliates


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