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.