2

Possible Duplicate:
Speed difference in using inline strings vs concatenation in php5?

Note - This was an interview question.

Given,

$a = "some text 1";
$b = "some text 2";

Which one of the following will be faster. Give some reason

$c = $a.$b;

or

$c = "$a$b";

I answered that first one will be faster because two variables are just appended. In the second case however there are variable replacements within a string. But I am not sure.

Community
  • 1
  • 1
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144
  • http://stackoverflow.com/questions/13620/speed-difference-in-using-inline-strings-vs-concatenation-in-php5 – shmeeps May 05 '11 at 20:53
  • 7
    Benchmark it, the difference will be negligible. Regardless, the important answer is "it doesn't matter". Among the worst and most pointless interview questions I've ever seen. – user229044 May 05 '11 at 20:53
  • 1
    You should think about _real_ problems and not micro optimizations. I predict, that you will never feel any difference ;) – KingCrunch May 05 '11 at 20:54
  • 3
    In 10000 runs, the first takes 0.0015 seconds, the second 0.0022 seconds. – Rich Bradshaw May 05 '11 at 20:56
  • 2
    To all (@Pekka, @KingCrunch and others) who are saying this does not matter, I completely agree with you guys but this was an interview question, so it matters there!! :) – Sandeepan Nath May 05 '11 at 20:57
  • 3
    Ask the interviewer why they would bother with such a pointless optimization. – Justin Morgan May 05 '11 at 21:01
  • 1
    @Justin Morgan - having said that I completely agree with you guys, I think these questions can give some idea about the reasoning of a candidate, his sanity/state of mind etc. – Sandeepan Nath May 05 '11 at 21:05
  • 1
    Such thoughts pollute ones code with the evil idea of hyper-performance by micro optimization, that will nobody else ever notice. Sorry, but I cannot say anything different than: You should _never_ care about something like this. – KingCrunch May 05 '11 at 21:09
  • I was dead serious. Sometimes when I interview candidates, I'll ask a ridiculous question like this to see if they call me out on it. If they actually try to answer it instead of probing further or challenging it, that's a mark against. I want people who aren't afraid to point out when something is dumb. – Justin Morgan May 05 '11 at 21:11
  • 1
    Lastly, I also ask others to not worry about these things while coding. If you still have an itching to apply some of these, make sure you are not wasting precious time or adding to any kind of lack of readability etc. After all it lies on a developer's mind control. I personally have never bothered to apply these while coding. But when asked in an interview, I think out of curiosity I like that, because most of the times they ask me unknown/new things. This was asked during a written test. If it were an interview I would have probably said it does not matter much, along with the answer. – Sandeepan Nath May 05 '11 at 21:21

2 Answers2

5

It does not matter. The speed difference will be a few microseconds. It will never have any real-world impact whatsoever.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

I would say option A

For basically the same reason you stated

Naftali
  • 144,921
  • 39
  • 244
  • 303