English 中文(简体)
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 ?

最佳回答

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

?>
问题回答

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

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.

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.

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 ( , 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.

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.

there is NO difference, period. ;)





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签