views:

487

answers:

7

Is there a speed difference between, say:

$newstring = "$a and $b went out to see $c";

and

$newstring = $a . " and " . $b . " went out to see " . $c;

and if so, why ?

+1  A: 

Yes there is, however the difference is very negligible between

$newstring = "$a and $b went out to see $c";

and

$newstring = $a . " and " . $b . " went out to see " . $c;

If you used:

$newstring = $a . ' and ' . $b . ' went out to see ' . $c;

The difference would be slightly bigger (but probably still negligible), the reason for this is, if I recall correctly (I may be wrong on this), that PHP scans and parses the contents within double quotation marks for variables and special characters (\t, \n and so on) and when using single quotation marks it doesn't parse for variables or special characters, so there may be a slight increase in speed.

Sbm007
From what i know about string parsing this is a myth, that single quotes would be faster. In my experience it's the opposite.
Peter Lindqvist
+8  A: 

Depending on the PHP version, it varies by how much the second is faster if you write it like: $newstring = $a . ' and ' . $b . ' went out to see ' . $c;

PHP is very inconsistent from version to version and build to build when it comes to performance, you have to test it for yourself. What nees to be said is that it also depends on the type of $a, $b and $c, as you can see below.

When you use ", PHP parses the string to see if there are any variable/placeholders used inside of it, but if you use only ' PHP treats it as a simple string without any further processing. So generally ' should be faster. At least in theory. In practice you must test.


Results(in seconds):

a, b, c are integers:
all inside "     : 1.2370789051056
split up using " : 1.2362520694733
split up using ' : 1.2344131469727

a, b, c are strings:
all inside "     : 0.67671513557434
split up using " : 0.7719099521637
split up using ' : 0.78600907325745  <--- this is always the slowest in the group. PHP, 'nough said

Using this code with Zend Server CE PHP 5.3:

<?php

echo 'a, b, c are integers:<br />';
$a = $b = $c = 123;

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = "$a and $b went out to see $c";
$t = xdebug_time_index() - $t;
echo 'all inside " : ', $t, '<br />';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = $a . " and " . $b . " went out to see " . $c;
$t = xdebug_time_index() - $t;
echo 'split up using " : ', $t, '<br />';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = $a . ' and ' . $b . ' went out to see ' . $c;
$t = xdebug_time_index() - $t;
echo 'split up using \' : ', $t, '<br /><br />a, b, c are strings:<br />';

$a = $b = $c = '123';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = "$a and $b went out to see $c";
$t = xdebug_time_index() - $t;
echo 'all inside " : ', $t, '<br />';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = $a . " and " . $b . " went out to see " . $c;
$t = xdebug_time_index() - $t;
echo 'split up using " : ', $t, '<br />';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = $a . ' and ' . $b . ' went out to see ' . $c;
$t = xdebug_time_index() - $t;
echo 'split up using \' : ', $t, '<br />';

?>
Marius Burz
+5  A: 

There likely will be a speed difference, since it's two different syntaxes. What you need to ask is if the difference is important. In this case, no, I don't think you need to be worried. The difference would be too negligible.

I would recommend you doing whatever makes most sense to you visually. "$a and $b went out to see $c" can be a bit confusing when looking at it. If you wanted to go that route, I'd suggest curly-braces around your variables: "{$a} and {$b} went out to see {$c}".

Jonathan Sampson
By the principle of least surprise, two different syntaxes that mean exactly the same thing should have the same properties. The fact that it might not in PHP is just a bad property of PHP.
Ira Baxter
@Ira: It's even worse... see my results below. What might be the fastest in one case might just as well be the slowest in another.
Marius Burz
A: 

Why don't you test it, and compare the difference? Numbers don't lie, if you find that one performs better than the other, then you should ask why.

David_001
Don't know why this answer got downvoted. It's the best advice when you use PHP.
Marius Burz
Because it wasn't a helpful answer. I already knew I could try it myself. I posted it here to see what other people had to say.
J. Stoever
+2  A: 

I did a quick benchmark, and as others have said, the results were very inconsistent. I didn't notice any performance gain using single quotes instead of double ones. My guess is that it all comes down to preference.

You may want to stick to one type of quote for your coding style, and if you do, choose double quotes. The replacement feature comes in handy more often than you'd think.

I put the benchmark code on github.

Scavenger
+3  A: 

If you're concerned about the speed of string concatenations at this level, you are using the wrong language. Compile an application in C for this use case and call that in your PHP script, if this really is a bottleneck.

soulmerge
A: 

there is NO difference, period. ;)

stereofrog
There sure is, but not an important one. Nor is it consistent from one PHP version to another.
Marius Burz