Skip to content

Instantly share code, notes, and snippets.

@sarfraznawaz2005
Last active October 12, 2015 18:32
Show Gist options
  • Save sarfraznawaz2005/a5f0245ed81c7cd089d4 to your computer and use it in GitHub Desktop.
Save sarfraznawaz2005/a5f0245ed81c7cd089d4 to your computer and use it in GitHub Desktop.
Simple function to benchmark code
<?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