Skip to content

Instantly share code, notes, and snippets.

@fabiocicerchia
Last active December 24, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiocicerchia/6775559 to your computer and use it in GitHub Desktop.
Save fabiocicerchia/6775559 to your computer and use it in GitHub Desktop.
PHP - Performance Benchmark (Skeleton to compare two algorithms)
<?php
function test1() {
}
function test2() {
}
function init() {
}
function destroy() {
}
function run($code, $max) {
$start = microtime(true);
for ($i = 0; $i < $max; $i++) {
$code();
}
$end = microtime(true);
return ($end - $start);
}
function benchmark($code, $times) {
init();
$executionTime = array();
for ($i = 0; $i < 5; $i++) {
$executionTime[] = run($code, $times);
}
sort($executionTime);
unset($executionTime[0]); // Remove the best
unset($executionTime[4]); // Remove the worst
$executionTime = array_sum($executionTime) / 3;
destroy();
return $executionTime;
}
$times = intval($_SERVER['argv'][1]);
$executionTime = benchmark('test1', $times);
echo "To run the code #1 $times times it took $executionTime seconds\n";
$executionTime = benchmark('test2', $times);
echo "To run the code #2 $times times it took $executionTime seconds\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment