Return to Snippet

Revision: 9224
at October 26, 2008 18:44 by iTony


Initial Code
<?php
/*
* Function: benchmark
* Author: Michael Bailey <[email protected]>
* 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;
}
?>

Initial URL
http://www.codewalkers.com/c/a/Miscellaneous-Code/Quick-n-Easy-Benchmark/

Initial Description

                                

Initial Title
Benchmark function for php

Initial Tags
php, function

Initial Language
PHP