[ACCEPTED]-Speed difference in using inline strings vs concatenation in php5?-performance

Accepted answer
Score: 109

The performance difference has been irrelevant since 15 at least January 2012, and likely earlier:

Single quotes: 0.061846971511841 seconds
Double quotes: 0.061599016189575 seconds

Earlier 14 versions of PHP may have had a difference 13 - I personally prefer single quotes to double 12 quotes, so it was a convenient difference. The 11 conclusion of the article makes an excellent 10 point:

Never trust a statistic you didn’t 9 forge yourself.

(Although the article quotes 8 the phrase, the original quip was likely 7 falsely attributed to Winston Churchill, invented 6 by Joseph Goebbels' propaganda ministry 5 to portray Churchill as a liar:

Ich traue 4 keiner Statistik, die ich nicht selbst gefälscht 3 habe.

This loosely translates to, "I 2 do not trust a statistic that I did not 1 fake myself.")

Score: 47

Well, as with all "What might be faster 19 in real life" questions, you can't beat 18 a real life test.

function timeFunc($function, $runs)
{
  $times = array();

  for ($i = 0; $i < $runs; $i++)
  {
    $time = microtime();
    call_user_func($function);
    $times[$i] = microtime() - $time;
  }

  return array_sum($times) / $runs;
}

function Method1()
{ 
  $foo = 'some words';
  for ($i = 0; $i < 10000; $i++)
    $t = "these are $foo";
}

function Method2()
{
  $foo = 'some words';
  for ($i = 0; $i < 10000; $i++)
    $t = "these are {$foo}";
}

function Method3()
 {
  $foo = 'some words';
  for ($i = 0; $i < 10000; $i++)
    $t = "these are " . $foo;
}

print timeFunc('Method1', 10) . "\n";
print timeFunc('Method2', 10) . "\n";
print timeFunc('Method3', 10) . "\n";

Give it a few runs to page 17 everything in, then...

0.0035568

0.0035388

0.0025394

So, as 16 expected, the interpolation are virtually 15 identical (noise level differences, probably 14 due to the extra characters the interpolation 13 engine needs to handle). Straight up concatenation 12 is about 66% of the speed, which is no great 11 shock. The interpolation parser will look, find 10 nothing to do, then finish with a simple 9 internal string concat. Even if the concat 8 were expensive, the interpolator will still 7 have to do it, after all the work to parse out 6 the variable and trim/copy up the original 5 string.

Updates By Somnath:

I added Method4() to above real time 4 logic.

function Method4()
 {
  $foo = 'some words';
  for ($i = 0; $i < 10000; $i++)
    $t = 'these are ' . $foo;
}

print timeFunc('Method4', 10) . "\n";

Results were:

0.0014739
0.0015574
0.0011955
0.001169

When you are just declaring a string 3 only and no need to parse that string too, then 2 why to confuse PHP debugger to parse. I 1 hope you got my point.

Score: 25

Live benchmarks:

http://phpbench.com/

There is actually a subtle 2 difference when concatenating variables 1 with single vs double quotes.

Score: 18

@Adam's test used

"these are " . $foo

note that the following 4 is even faster:

'these are ' . $foo;

this is due to the fact, that 3 a double quoted "string" gets evaluated, where 2 a single quoted 'string' is just taken as 1 is...

Score: 12

Don't get too caught up on trying to optimize 15 string operations in PHP. Concatenation 14 vs. interpolation is meaningless (in real 13 world performance) if your database queries 12 are poorly written or you aren't using any 11 kind of caching scheme. Write your string 10 operations in such a way that debugging 9 your code later will be easy, the performance 8 differences are negligible.

@uberfuzzy Assuming 7 this is just a question about language minutia, I 6 suppose it's fine. I'm just trying to add 5 to the conversation that comparing performance 4 between single-quote, double-quote and heredoc 3 in real world applications in meaningless 2 when compared to the real performance sinks, such 1 as poor database queries.

Score: 9

Any differences in execution time are completely 8 negligible.

Please see

Don't waste time on 7 micro-optimizations like this. Use a profiler 6 to measure the performance of your application 5 in a real world scenario and then optimize 4 where it is really needed. Optimising a 3 single sloppy DB query is likely to make 2 a bigger performance improvement than applying 1 micro-optimisations all over your code.

Score: 4

there is a difference when concatenating 16 variables... and what you are doing with 15 the result... and if what you are doing 14 is dumping it to output, is or isn't output 13 buffering on.

also, what is the memory situation 12 of the server? typically memory management 11 on a higher level platform is worse than 10 that at lower platforms...

$a = 'parse' . $this; 

is managing memory 9 at the user code platform level...

$a = "parse $this";

is managing 8 memory at the php system code platform level...

so 7 these benchmarks as related to CPU don't 6 tell the full story.

running the benchmark 5 1000 times vs running the benchmark 1000 4 times on a server that is attempting to 3 run that same simulation 1000 times concurrently... you 2 might get drastically different results 1 depending on the scope of the application.

Score: 3

I seem to remember that the developer of 6 the forum software, Vanilla replaced all 5 the double quotes in his code with single 4 quotes and noticed a reasonable amount of 3 performance increase.

I can't seem to track 2 down a link to the discussion at the moment 1 though.

Score: 2

Just to add something else to the mix, if 3 you are using a variable inside a double 2 quoted string syntax:

$foo = "hello {$bar}";

is faster than

$foo = "hello $bar";

and both 1 of these are faster than

$foo = 'hello' . $bar; 
Score: 1

Double quotes can be much slower. I read 3 from several places that that it is better 2 to do this

'parse me '.$i.' times'

than

"parse me $i times"

Although I'd say the second 1 one gave you more readable code.

Score: 0

Practically there is no difference at all! See 1 the timings: http://micro-optimization.com/single-vs-double-quotes

Score: 0

It should be noted that, when using a modified 16 version of the example by Adam Wright with 15 3 variables, the results are reversed and 14 the first two functions are actually faster, consistently. This 13 is with PHP 7.1 on CLI:

function timeFunc($function, $runs)
{
    $times = array();

    for ($i = 0; $i < $runs; $i++)
    {
        $time = microtime();
        call_user_func($function);
        @$times[$i] = microtime() - $time;
    }

    return array_sum($times) / $runs;
}

function Method1()
{ 
    $foo = 'some words';
    $bar = 'other words';
    $bas = 3;
    for ($i = 0; $i < 10000; $i++)
         $t = "these are $foo, $bar and $bas";
}

function Method2()
{
    $foo = 'some words';
    $bar = 'other words';
    $bas = 3;
    for ($i = 0; $i < 10000; $i++)
         $t = "these are {$foo}, {$bar} and {$bas}";
}

function Method3()
{
    $foo = 'some words';
    $bar = 'other words';
    $bas = 3;
    for ($i = 0; $i < 10000; $i++)
         $t = "these are " . $foo . ", " . $bar . " and " .$bas;
}

print timeFunc('Method1', 10) . "\n";
print timeFunc('Method2', 10) . "\n";
print timeFunc('Method3', 10) . "\n";

I've also tried with 12 '3' instead of just the integer 3, but I 11 get the same kind of results.

With $bas = 3:

0.0016254
0.0015719
0.0019806

With 10 $bas = '3':

0.0016495
0.0015608
0.0022755

It should be noted that these 9 results vary highly (I get variations of 8 about 300%), but the averages seem relatively 7 steady and almost (9 out of 10 cases) always 6 show a faster execution for the 2 first 5 methods, with Method 2 always being slightly 4 faster than method 1.

In conclusion: what 3 is true for 1 single operation (be it interpolation 2 or concatenation) is not always true for 1 combined operations.

Score: 0

Yes, originally this is about PHP5, however 9 in few months arrive PHP8 and today the 8 best option tested over my PHP 7.4.5 is use PHP - Nowdoc (tested 7 over WIN 10 + Apache and CentOs 7 + Apache):

function Method6(){
    $k1 = 'AAA';
    for($i = 0; $i < 10000; $i ++)$t = <<<'EOF'
K1= 
EOF
.$k1.
<<<'EOF'
K2=
EOF
.$k1;
    }

here 6 the method #5 (using Heredoc to concatenat):

function Method5(){
    $k1 = 'AAA';
    for($i = 0; $i < 10000; $i ++)$t = <<<EOF
K1= $k1
EOF
.<<<EOF
K2=$k1 
EOF;
    }

the 5 methods 1 to 4 is in beginning of this post

In 4 all my tests the "winner" is method 3 #6 (Newdoc), no't very easy to read, but 2 very fast in CPU and ever using the function 1 function timeFunc($function) by @Adam Wright.

Score: 0

I have tested php 7.4 and php 5.4 with following 6 test cases, It was little still confusing 5 to me.

<?php
$start_time = microtime(true);
$result = "";
for ($i = 0; $i < 700000; $i++) {
    $result .= "THE STRING APPENDED IS " . $i;
    // AND $result .= 'THE STRING APPENDED IS ' . $i;
    // AND $result .= "THE STRING APPENDED IS $i";
}
echo $result;
$end_time = microtime(true);
echo "<br><br>";
echo ($end_time - $start_time) . " Seconds";

PHP 7.4 Outputs

 1. "THE STRING APPENDED IS " . $i = 0.16744208335876
 2. 'THE STRING APPENDED IS ' . $i = 0.16724419593811
 3. "THE STRING APPENDED IS $i" = 0.16815495491028

PHP 5.3 Outputs

 1. "THE STRING APPENDED IS " . $i = 0.27664494514465
 2. 'THE STRING APPENDED IS ' . $i = 0.27818703651428
 3. "THE STRING APPENDED IS $i" = 0.28839707374573

I have 4 tested so many times, In php 7.4 it seems 3 to be all 3 test cases got same result many 2 times but still concatenation have little 1 bittle advantage in performance.

Score: 0

Based on @adam-wright answer, I wanted to 9 know if speed difference happens without 8 no concataining / no vars in a string.

== My questions...

  • is $array['key'] call or set faster than $array["key"] !?
  • is $var = "some text"; slower than $var = 'some text'; ?

== My tests with 7 new vars every time to avoid use same memory 6 address :

function getArrDblQuote() { 
    $start1 = microtime(true);
    $array1 = array("key" => "value");
    for ($i = 0; $i < 10000000; $i++)
        $t1 = $array1["key"];
    echo microtime(true) - $start1;
}
function getArrSplQuote() {
    $start2 = microtime(true);
    $array2 = array('key' => 'value');
    for ($j = 0; $j < 10000000; $j++)
        $t2 = $array2['key'];
    echo microtime(true) - $start2;
}

function setArrDblQuote() { 
    $start3 = microtime(true);
    for ($k = 0; $k < 10000000; $k++)
        $array3 = array("key" => "value");
    echo microtime(true) - $start3;
}
function setArrSplQuote() {
    $start4 = microtime(true);
    for ($l = 0; $l < 10000000; $l++)
        $array4 = array('key' => 'value');
    echo microtime(true) - $start4;
}

function setStrDblQuote() { 
    $start5 = microtime(true);
    for ($m = 0; $m < 10000000; $m++)
        $var1 = "value";
    echo microtime(true) - $start5;
}
function setStrSplQuote() {
    $start6 = microtime(true);
    for ($n = 0; $n < 10000000; $n++)
        $var2 = 'value';
    echo microtime(true) - $start6;
}

print getArrDblQuote() . "\n<br>";
print getArrSplQuote() . "\n<br>";
print setArrDblQuote() . "\n<br>";
print setArrSplQuote() . "\n<br>";
print setStrDblQuote() . "\n<br>";
print setStrSplQuote() . "\n<br>";

== My Results :

array get double quote 2.1978828907013

array get single quote 5 2.0163490772247

array set double quote 1.9173440933228

array get single quote 1.4982950687408

var set 4 double quote 1.485809803009

var set single quote 1.3026781082153

== My conclusion !

So, result is that 3 difference is not very significant. However, on 2 a big project, I think it can make the difference 1 !

More Related questions