Search the Community
Showing results for tags 'perf'.
-
I was just curios about how fast PHP handles the different types of string concatenation/parsing with variables, because I saw Ryan's style is double quotes and I was thinking, isn't that slower than single quote concatenation? I was surprised because I remember to have learned that the additional parsing of double quote strings make the whole thing slower. I was wrong, here is my benchmark. Thought it'd be interesting to know. 1000000 times per test // initial object creation, only once $fooObject = new stdClass; $fooObject->foo = "Lorem ipsum dolor sit amet."; $fooObject->bar = "Consectetur adipisicing elit."; $fooObject->baz = "Sed do eiusmod tempor incididunt."; double quotes "foo: {$fooObject->foo}, bar: {$fooObject->bar}, baz: {$fooObject->baz}" 0.55859899520874 s single quotes concat 'foo: ' . $fooObject->foo . ', bar: ' . $fooObject->bar . ', baz: ' . $fooObject->baz 0.61702013015747 s sprintf sprintf("foo: %s, bar: %s, baz: %s", $fooObject->foo, $fooObject->bar, $fooObject->baz) 1.6139810085297 s array implode implode('', array('foo: ', $fooObject->foo, ', bar: ', $fooObject->bar, 'baz: ', $fooObject->baz)) 2.0107381343842 s Code: <?php $times = 1000000; ?> <h1>PHP string concat benchmark</h1> <p><?=$times?> times per test</p> <code><pre> $fooObject = new stdClass; $fooObject->foo = "Lorem ipsum dolor sit amet."; $fooObject->bar = "Consectetur adipisicing elit."; $fooObject->baz = "Sed do eiusmod tempor incididunt."; </pre></code><br> <hr> <?php function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } $fooObject = new stdClass; $fooObject->foo = "Lorem ipsum dolor sit amet."; $fooObject->bar = "Consectetur adipisicing elit."; $fooObject->baz = "Sed do eiusmod tempor incididunt."; $time_start = microtime_float(); for ($i=0; $i < $times; $i++) { $someString = "foo: {$fooObject->foo}, bar: {$fooObject->bar}, baz: {$fooObject->baz}"; } $time_end = microtime_float(); $time = $time_end - $time_start; ?> <h2>double quote</h2> <code>"foo: {$fooObject->foo}, bar: {$fooObject->bar}, baz: {$fooObject->baz}"</code><br><br> <?=$time?> s<br><br> <hr> <?php $time_start = microtime_float(); $someString = ''; for ($i=0; $i < $times; $i++) { $someString = 'foo: ' . $fooObject->foo . ', bar: ' . $fooObject->bar . ', baz: ' . $fooObject->baz; } $time_end = microtime_float(); $time = $time_end - $time_start; ?> <h2>single quote concat</h2> <code>'foo: ' . $fooObject->foo . ', bar: ' . $fooObject->bar . ', baz: ' . $fooObject->baz</code><br><br> <?=$time?> s<br><br> <hr> <?php $time_start = microtime_float(); for ($i=0; $i < $times; $i++) { $someString = sprintf("foo: %s, bar: %s, baz: %s", $fooObject->foo, $fooObject->bar, $fooObject->baz); } $time_end = microtime_float(); $time = $time_end - $time_start; ?> <h2>sprintf</h2> <code>sprintf("foo: %s, bar: %s, baz: %s", $fooObject->foo, $fooObject->bar, $fooObject->baz)</code><br><br> <?=$time?> s<br><br> <hr> <?php $time_start = microtime_float(); for ($i=0; $i < $times; $i++) { $someString = implode('', array('foo: ', $fooObject->foo, ', bar: ', $fooObject->bar, 'baz: ', $fooObject->baz)); } $time_end = microtime_float(); $time = $time_end - $time_start; ?> <h2>array implode</h2> <code>implode('', array('foo: ', $fooObject->foo, ', bar: ', $fooObject->bar, 'baz: ', $fooObject->baz))</code><br><br> <?=$time?> s<br><br>