Skip to content

Instantly share code, notes, and snippets.

@GhostToast
Last active December 30, 2015 11:59
Show Gist options
  • Save GhostToast/7826211 to your computer and use it in GitHub Desktop.
Save GhostToast/7826211 to your computer and use it in GitHub Desktop.
micro optimization!! each test now involves many tests, so average is calculated and indicated in comments. one test was run at a time.
<?php
$foo = 'foo';
$bar = 'bar';
$foobar = 'foobar';
$string = '';
$tests = array();
for ($z = 0; $z<1000; $z++){
$startTime = microtime(true);
for ($i = 0; $i<1000; $i++) {
// $string .= 'this ' . ' is ' . $foo . ', this is ' . $bar . ' and this is ' . $foobar; // 0.4931318783 Milliseconds
// $string .= "this is $foo, this is $bar and this is $foobar"; // 0.4040865907 Milliseconds
// $string .= "this is {$foo}, this is {$bar} and this is {$foobar}"; // 0.4096872842 Milliseconds
}
$tests[] = number_format(( microtime(true) - $startTime), 10) * 1000;
}
echo array_sum($tests) / count($tests);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment