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! |
Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
|
|
|
|
In this tutorial, you can learn how to install and configure the IBM Rational Asset Manager Eclipse client, explore the different views in the Asset Management perspective, learn various search techniques, work with existing assets, and submit a new asset. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement. FREE! Go There Now!
|
|
|
|
Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
|
|
|
|
Learn from the best! Find out how developers use Rational ClearCase to be more flexible, innovative and deliver higher quality code in the Rational ClearCase Power Users eKit. This complimentary eKit provides a collection of materials, like articles, whitepapers, and demos that can help you become a power user of Rational ClearCase. FREE! Go There Now!
|
|
|
|
As organizations integrate software into every aspect of business, they are constantly pressured to deliver faster, better, and cheaper results. Unfortunately, a “dis-integrated” software delivery approach reduces returns while increasing costs. This IBM Rational White Paper shows how Integrated Requirements Management aligns organizations around maximizing value and keeping pace with change. FREE! Go There Now!
|
|
|
|
Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository. FREE! Go There Now!
|
|
|
|
Whether you are creating new applications or modifying existing ones, managing integration of new components with traditional z/OS elements is a critical part of building and deploying modern applications. Listen to this webcast to see how IBM can help you optimize your development process using an IDE like Rational Developer for System z that integrates with management tools, such as ClearCase to manage your application development on mainframes. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. 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!
|
|
|
|
All FREE IBM® developerWorks Tools! |