Speed difference in using inline strings vs concat

2019-01-01 06:41发布

(assume php5) consider

<?php

    $foo = 'some words';

    //case 1
    print "these are $foo";

    //case 2
    print "these are {$foo}";

    //case 3
    print 'these are ' . $foo;
?>

Is there much of a difference between 1 and 2?

If not, what about between 1/2 and 3?

12条回答
若你有天会懂
2楼-- · 2019-01-01 07:21

Any differences in execution time are completely negligible.

Please see

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

查看更多
梦醉为红颜
3楼-- · 2019-01-01 07:23

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

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

$a = 'parse' . $this; 

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

$a = "parse $this";

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

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

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

查看更多
大哥的爱人
4楼-- · 2019-01-01 07:24

It should be noted that, when using a modified version of the example by Adam Wright with 3 variables, the results are reversed and the first two functions are actually faster, consistently. This 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 '3' instead of just the integer 3, but I get the same kind of results.

With $bas = 3:

0.0016254
0.0015719
0.0019806

With $bas = '3':

0.0016495
0.0015608
0.0022755

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

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

查看更多
笑指拈花
5楼-- · 2019-01-01 07:29

Live benchmarks:

http://phpbench.com/

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

查看更多
浮光初槿花落
6楼-- · 2019-01-01 07:30

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

查看更多
十年一品温如言
7楼-- · 2019-01-01 07:34

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

'parse me '.$i.' times'

than

"parse me $i times"

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

查看更多
登录 后发表回答