Last active
July 1, 2018 00:33
-
-
Save TheEyesightDim/8caffeb575bf8d3f647f8cdd8e205bbc to your computer and use it in GitHub Desktop.
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 | |
require 'vendor/autoload.php'; | |
use Symfony\Component\Stopwatch\Stopwatch; | |
/** | |
* @param callable $callable is a function or functor callable with parentheses. | |
* @param array ...$args is the argument list which will be supplied to the $callable function. | |
* @return float|int which represents the time elapsed in the function called, in milliseconds. | |
*/ | |
function benchmarker(Callable $callable, ...$args){ | |
$stopwatch = new Stopwatch(true); | |
$stopwatch->start('benchmark'); | |
$callable(...$args); | |
$stopwatch->stop('benchmark'); | |
$result = $stopwatch->getEvent('benchmark'); | |
return $result->getDuration(); | |
} | |
$func = function ($size){ | |
$arr = array(); | |
for($i = 0; $i < $size; ++$i){ | |
$arr[$i] = $i+1; | |
echo "Slot $i filled...\n"; | |
} | |
}; | |
echo benchmarker($func,10)."ms elapsed."; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A very simple benchmarking function using Symfony's stopwatch to measure the execution time in milliseconds of any single function.