Created
April 16, 2010 15:51
-
-
Save predominant/368602 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Configuration options | |
* | |
*/ | |
$iterations = 10000000; | |
info(); | |
/** | |
* Actual Benchmarks to run | |
* | |
*/ | |
benchmark('!bool', $iterations, function() { | |
if (!false) {} | |
}); | |
benchmark('bool === bool', $iterations, function() { | |
if (false === false) {} | |
}); | |
// ---------------------------------------- | |
// ---------------------------------------- | |
// Modification below here not necessary, | |
// unless you're correcting some code that | |
// I fucked up. | |
// ---------------------------------------- | |
// ---------------------------------------- | |
/** | |
* Environment Information and Test Result header | |
* | |
* @return void | |
*/ | |
function info() { | |
echo "/**\n"; | |
echo " * ----------------------------------------\n"; | |
echo " * PHP " . phpversion() . " (" . php_sapi_name() . ")\n"; | |
echo " * Memory: " . (memory_get_peak_usage(true) / 1024 / 1024) . "MB / " . ini_get('memory_limit') . "\n"; | |
echo " * ----------------------------------------\n"; | |
echo " */\n\n"; | |
} | |
/** | |
* Generic Benchmarking Function | |
* | |
* @param string $title Title of the test set | |
* @param string $iterations iterations to perform | |
* @param string $code Code to run | |
* @return void | |
*/ | |
function benchmark($title, $iterations, $code) { | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
$code(); | |
} | |
$elapsed = microtime(true) - $start; | |
echo "$title\n => $elapsed\n"; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment