Last active
October 12, 2015 18:32
-
-
Save sarfraznawaz2005/a5f0245ed81c7cd089d4 to your computer and use it in GitHub Desktop.
Simple function to benchmark code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function benchmark($function, $args = null, $iterations = 1, $timeout = 0) | |
{ | |
set_time_limit($timeout); | |
if (is_callable($function) === true) { | |
list($usec, $sec) = explode(" ", microtime()); | |
$start = ((float)$usec + (float)$sec); | |
for ($i = 1; $i <= $iterations; ++$i) { | |
call_user_func_array($function, (array)$args); | |
} | |
list($usec, $sec) = explode(" ", microtime()); | |
$end = ((float)$usec + (float)$sec); | |
return round(($end - $start), 4); | |
} | |
return false; | |
} | |
// usage | |
$results = array(); | |
$results[] = benchmark('myfunc', 'arg1', 10); | |
$results[] = benchmark('myfunc2', array('arg1', 'arg2')); | |
$results[] = benchmark('myfunc3'); | |
print_r($results); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment