Last active
December 24, 2015 09:09
-
-
Save fabiocicerchia/6775559 to your computer and use it in GitHub Desktop.
PHP - Performance Benchmark (Skeleton to compare two algorithms)
This file contains hidden or 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 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