Skip to content

Instantly share code, notes, and snippets.

@predominant
Created April 16, 2010 15:51
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 predominant/368602 to your computer and use it in GitHub Desktop.
Save predominant/368602 to your computer and use it in GitHub Desktop.
<?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