views:

913

answers:

6

Hello there,

This post is not really a question, but it could be useful to share some coding tips.

Here is the one I'de like to share with you. I'm gonna show you 4 examples to do the same thing, but only the last one will be the best.

$foo = 'John SMITH';

echo "Hello $foo, welcome on my website.";

echo "Hello " . $foo . " welcome on my website.";

echo 'Hello ' . $foo . ' welcome on my website.';

echo 'Hello ', $foo , ' welcome on my website.';

I'm sure you all know that echo '$foo' won't work, but still, I'm pretty sure that you use double quote to display a simple information. THIS IS BAD.

Well let's begin : The first one is bad (as well as the second) because using double quote forces php to scan the string to look for a substitution to be done (I mean a variable).

The second one is a little better, since php has no replacement to do.

The third one, is better because of simple quote, so that the language knows he can just send the text without processing, but the "bad" thing is the use of concatenation (dot operator, like in the second example).

The last one uses simple quote, and the coma operator. Why is this solution better?

Well, what happens when Using the third solution?

php creates a string, containing "Hello ", then it has to enlarge it, to put the content of foo variable ("John SMITH"), and then, enlarge it again to put " Welcome on my website." sentence. Then, echo can use this, to ... echo it :)

Whereas in the 4th one, the only thing to do for echo is to send "Hello ", then $foo's content, then " Welcome on my website." to the output, and that is all! Because echo just has to send the text, without creating a string that will have to be enlarged to contain the whole texte (that would have been concate, which has to be grown (because of concatenation) and then displayed.

I'll try to find back some benchmarks and put them here.

Fell free to comment or react, and excuse my english, this is not my mother thong.

+4  A: 

It's not worth it. You may experience some 0,1% speedup (don't take it literally; a negligible speedup) for your whole application even if you manage to make string concatenation faster by 30%. Most of the time will be spent in DB queries or more sophisticated logic.

phjr
+6  A: 

In practice, this almost always isn't going to be your primary bottleneck.

http://www.procata.com/blog/archives/2005/03/08/microbenchmarks-of-single-and-double-qouting/

Paul Dixon
+14  A: 

It is completely irrelevant which solution performs better. Computers are blazing fast and CPU cycles are cheap, therefore millisecond optimizations are pointless. The only real optimization is the one that helps you read the code faster, so you can save your precious time and focus on actually delivering working code.

Michał Rudnicki
+14  A: 

Creating a small (but still sucking :) benchmark and running it yields the following results:

0.0022029876709
0.00211095809937
0.00213599205017
0.00551700592041

The last method is clearly the slowest.

Bombe
+6  A: 

The three rules of optimization are:

  1. Don't Optimize Yet.
  2. Don't Optimize Yet.
  3. Don't Optimize Yet.

Save it for the LAST thing you do, only after learning where the slowdowns are.

Otherwise, you will be putting the cart before the horse, so to speak.

Genericrich
I hear you, but if changing one of your habits results in faster code, why not do it? It isn't harder to write or maintain, you are just doing things the right way first.
James McMahon
You're spent more time reading these comments than will ever by saved across all the apps you ever write in your entire lifetime by switching from " to '.
Preston
A: 

$a = "Hello ";

$b = "World";

(a) $c = $a.$b;

(b) $c = "$a$b";

I would like to know which one is faster to concat (a) or (b). OR if there is another faster way to contact, let me know.

zawmn83
And if Stack Overflow was a PHP interpreter you would already have the answer. Alternatively you could simply *read* what is written all over this page: DON’T BOTHER OPTIMIZING STUFF LIKE THAT, IT’S NOT WORTH IT.
Bombe