4

Talking about PHP, i would like to ask if there is a difference in performance between these two:

$name=($IsBoy)?"George":"Mary";

vs

if($IsBoy)
{
    $name="George";
}
else
{
    $name="Mary";
}
  • Will these two result to different opcode?

  • If yes, Would be any theoretical difference in performance? (of course ignore the time that these two needs to be read / compiled / interpreted)

  • If, yes, then do optimizers like zend optimizer take advantage of this and do any re-arrangements automatically?

p.s. if you believe that my code for the "full-longhand" if-then-else is too complex, please provide an example of the most basic code and answer on that.

UPDATE:

I hoped the question is perfectly clear, but it seems people do not get the message. This question is about the THEORETICAL (...yet real and mesaureable) difference in performance (thats why i applied bold and italic in theoretical). Please do not answer by saying what programming style is more readable and that this is too nitpicking to worry about performance.

p.s. 2: by giving emphasis to the word theoretical i try to prevent answers of the type "don't worry its not worth the trouble, its just nanoseconds."

Sharky
  • 6,154
  • 3
  • 39
  • 72
  • I don't know for any certainty but my guess is that there is no real performance difference between the two and if there is it would be a micro-optimization which you shouldn't worry about. – chrislondon Jul 01 '13 at 12:55
  • 2
    You can check this: http://fabien.potencier.org/article/48/the-php-ternary-operator-fast-or-not – Kristian Vitozev Jul 01 '13 at 12:56
  • Fabien's article pretty much matches my own observations as well; though it's nice to see an explanation about why – Mark Baker Jul 01 '13 at 12:58
  • Doubt it. But even if there is, the difference will be small it won't be worth worrying about. – Terry Harvey Jul 01 '13 at 13:00
  • @KristianVitozev that article is really informative, thanks! – Sharky Jul 01 '13 at 13:07
  • Since you're not worried about real performance and only theoretical performance, sounds like this isn't a real question. It's only a theorectical one. – Expedito Jul 01 '13 at 16:10
  • @PédeLeão i don't understand the point of your comment, also your definition of "real" and your conclusion that this is a question, but not a "real" question (?) – Sharky Jul 01 '13 at 16:26
  • And I don't understand the point of you question. You might check out the question guidelines where it says, "You should only ask practical, answerable questions..." http://stackoverflow.com/unanswered. You question seems to fail in that regard. – Expedito Jul 01 '13 at 16:43
  • 1
    @PédeLeão it is practical, because it has applications in real life, and is completely answerable. i asked 1) about the produced opcode (this is not something abstract) 2) i asked about mesaurements for the performance (real life measurements) and 3) i asked about the zend optimizer (is this practical enough for you?). if you believe this answer fails you can flag it. i posted code for god's sakes. – Sharky Jul 01 '13 at 16:55
  • 1
    The Zend Optimizer is about improving code in a real way, not just theoretical. When you start talking about the performance results of nanoseconds, you convincing me all the more that your question has no practical purpose. – Expedito Jul 01 '13 at 17:14

3 Answers3

5

OK this quite interesting

i have did a quick x debug test for your tow examples respectively

and here what i have got

enter image description here

enter image description here

Sure you may have a different result with each refresh put all of them ensure that

Second method is better than first always

even though with each refresh you may find first method take less time occasionally but this is something related with PHP core optimization

regarding to Zend optimizer i didn't test that

Samy Massoud
  • 4,295
  • 2
  • 35
  • 48
0

I often discard if-then-else in benefit of:

$name = 'the expected';
if ([expr]) $name = 'the exception';

Easy to read, less braces and compact.

Teson
  • 6,644
  • 8
  • 46
  • 69
  • thats pretty nice, i do it too, but its not an answer to my question. – Sharky Jul 01 '13 at 14:15
  • But, this is less performant since you're making the engine do more steps than necessary. What I mean by that is your method will always do the following: 1. Set the variable. 2. check a condition 3. Unset the variable 4. Set the new value and type to the variable. The IF / ELSE condition will have less steps. 1. Check condition, go to either line 2. Unset 3. Set No 4th step. So, you're saving a bit of processing when setting the first variable. 25% to be exact. – Webmaster G Nov 07 '19 at 14:39
-1

Both normal ifelse statement and a ternary operator have a slight perfomance difference.You can check the below stackoverflow links for more reference

Click here

Community
  • 1
  • 1
Jijo John
  • 1,375
  • 9
  • 21