Last active
March 5, 2018 11:40
-
-
Save lorenzo/ff52152f6374c20d55d03780b6ea6858 to your computer and use it in GitHub Desktop.
This file contains 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 = 1000000; | |
info(); | |
/** | |
* Actual Benchmarks to run | |
* | |
*/ | |
$strings = 'not used'; | |
benchmark('cast', $iterations, function () use ($strings) { | |
$a = (int)"134.1"; | |
}); | |
benchmark('coerce + cast', $iterations, function () use ($strings) { | |
$a = (int) + "134.1"; | |
}); | |
benchmark('cast simple', $iterations, function () use ($strings) { | |
$a = (int)"134"; | |
}); | |
benchmark('coerce + cast simple', $iterations, function () use ($strings) { | |
$a = (int) + "134"; | |
}); | |
// ---------------------------------------- | |
// ---------------------------------------- | |
// 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
Run 3x