J. Stoever
J. Stoever

Reputation: 866

PHP String concatenation - "$a $b" vs $a . " " . $b - performance

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 ?

Upvotes: 4

Views: 3441

Answers (7)

user187291
user187291

Reputation: 53940

there is NO difference, period. ;)

Upvotes: -2

Marius Burz
Marius Burz

Reputation: 4645

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 />';

?>

Upvotes: 16

soulmerge
soulmerge

Reputation: 75704

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.

Upvotes: 2

Wander Nauta
Wander Nauta

Reputation: 19615

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.

Upvotes: 2

David_001
David_001

Reputation: 5802

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.

Upvotes: 1

Sampson
Sampson

Reputation: 268344

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}".

Upvotes: 6

Waleed Amjad
Waleed Amjad

Reputation: 6808

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.

Upvotes: 1

Related Questions