| | |||||||
| |||||||
| |||||||
|
|
|
|
|
|
|
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 <?php /** * 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 (jinxidoru@byu.net) * 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); } } ?>
More Date Time Code Articles |
| |
| |