Skip to content

Instantly share code, notes, and snippets.

@lorenzo
Last active March 5, 2018 11:40
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 lorenzo/ff52152f6374c20d55d03780b6ea6858 to your computer and use it in GitHub Desktop.
Save lorenzo/ff52152f6374c20d55d03780b6ea6858 to your computer and use it in GitHub Desktop.
<?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";
};
@dereuromark
Copy link

dereuromark commented Mar 5, 2018

/**
 * ----------------------------------------
 * PHP 7.2.2-3+ubuntu16.04.1+deb.sury.org+1 (cli)
 * Memory: 2MB / -1
 * ----------------------------------------
 */

cast
   => 0.076174974441528
coerce + cast
   => 0.045148849487305
cast simple
   => 0.05767297744751
coerce + cast simple
   => 0.043489933013916


/**
 * ----------------------------------------
 * PHP 7.2.2-3+ubuntu16.04.1+deb.sury.org+1 (cli)
 * Memory: 2MB / -1
 * ----------------------------------------
 */

cast
   => 0.074118852615356
coerce + cast
   => 0.046221017837524
cast simple
   => 0.054520845413208
coerce + cast simple
   => 0.041806936264038

/**
 * ----------------------------------------
 * PHP 7.2.2-3+ubuntu16.04.1+deb.sury.org+1 (cli)
 * Memory: 2MB / -1
 * ----------------------------------------
 */

cast
   => 0.073346853256226
coerce + cast
   => 0.045939922332764
cast simple
   => 0.056981086730957
coerce + cast simple
   => 0.0400071144104

Run 3x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment