Skip to content

Instantly share code, notes, and snippets.

@TheEyesightDim
Last active July 1, 2018 00:33
Show Gist options
  • Save TheEyesightDim/8caffeb575bf8d3f647f8cdd8e205bbc to your computer and use it in GitHub Desktop.
Save TheEyesightDim/8caffeb575bf8d3f647f8cdd8e205bbc to your computer and use it in GitHub Desktop.
<?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.";
@TheEyesightDim
Copy link
Author

A very simple benchmarking function using Symfony's stopwatch to measure the execution time in milliseconds of any single function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment