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);
}
}
?>
| 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
developerWorks - FREE Tools! |
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects. FREE! Go There Now!
|
|
|
|
Manage, govern, and share services across your organization by using WebSphere Service Registry and Repository. Follow the hands-on exercises to learn how to navigate the Web interface to publish, find, reuse, and update services. FREE! Go There Now!
|
|
|
|
Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7. FREE! Go There Now!
|
|
|
|
Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered! FREE! Go There Now!
|
|
|
|
Join this webcast to discover the key requirements for successful change and release management. Learn how to extend your .NET environment to improve productivity and collaboration, and address core problems afflicting team development. In this webcast, we’ll review typical challenges faced by customers and how to resolve them with the IBM Rational Change and Release Management solution, including Rational ClearCase, Rational ClearQuest and Rational Build Forge. Replay is available for 9 months. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity. FREE! Go There Now!
|
|
|
|
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |