简体   繁体   English

简写如果然后在PHP中的性能/优化

[英]Shorthand if then else performance/optimization in php

Talking about PHP, i would like to ask if there is a difference in performance between these two: 谈到PHP,我想问一下这两者之间的性能是否存在差异:

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

vs 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? 如果,是的,那么像zend optimizer这样的优化器会利用这个并自动进行任何重新安排吗?

ps 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. ps如果你认为我的代码为“全长”if-then-else太复杂了,请提供一个最基本的代码和答案的例子。

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. 请不要回答说什么编程风格更具可读性,而且这太过挑剔而不用担心性能问题。

ps 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." ps 2:通过强调理论这个词我试图阻止类型的答案“不要担心它不值得麻烦,它只是纳秒。”

OK this quite interesting 好的,这非常有趣

i have did a quick x debug test for your tow examples respectively 我已经分别对你的两个例子进行了快速x调试

and here what i have got 在这里我得到了什么

在此输入图像描述

在此输入图像描述

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 即使每次刷新,你可能会发现第一种方法偶尔花费的时间较少,但这与PHP核心优化有关

regarding to Zend optimizer i didn't test that 关于Zend优化器,我没有测试

I often discard if-then-else in benefit of: 我常常放弃if-then-else以获益:

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

Easy to read, less braces and compact. 易于阅读,支撑更少,更紧凑。

Both normal ifelse statement and a ternary operator have a slight perfomance difference.You can check the below stackoverflow links for more reference 正常的ifelse语句和三元运算符都有轻微的性能差异。您可以查看下面的stackoverflow链接以获取更多参考

Click here 点击这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM