drogon982007 2016-04-20 00:45
浏览 332
已采纳

为什么嵌套的if语句比使用&&运算符更快?

Actually I'm wonder, why when I use multiple conditions into one if-statement is slower than multiple separated if-statement? In other word, why using && is slow?

Consider this:

$a = 1;
$b = 2;
$c = 3;
$d = 4;
$e = 5;

So the execution's time of this: 0.32209205627441

if ( $a == 1 && $b == 2 && $c == 3 && $d == 4 && $e == 5 ) {}

And this: 0.25026607513428

if ( $a == 1 ){
    if ( $b == 2 ) {
        if ( $c == 3 ) {
            if ( $d == 4 ) {
                if ( $e == 5 ) {}
            }
        }
    }
}

Online Benchmark

  • 写回答

1条回答 默认 最新

  • doubaolan2842 2016-04-20 00:59
    关注

    One obvious difference is that the long chained expression performs an extra cast to bool after every comparison (since the && operator requires that):

      11    11    >   IS_EQUAL                                         ~16     !0, 1
            12      > JMPZ_EX                                          ~16     ~16, ->15
            13    >   IS_EQUAL                                         ~17     !1, 2
            14        BOOL                                             ~16     ~17
            15    > > JMPZ_EX                                          ~16     ~16, ->18
            16    >   IS_EQUAL                                         ~18     !2, 3
            17        BOOL                                             ~16     ~18
            18    > > JMPZ_EX                                          ~16     ~16, ->21
            19    >   IS_EQUAL                                         ~19     !3, 4
            20        BOOL                                             ~16     ~19
            21    > > JMPZ_EX                                          ~16     ~16, ->24
            22    >   IS_EQUAL                                         ~20     !4, 5
            23        BOOL                                             ~16     ~20
            24    > > JMPZ                                                     ~16, ->25
    

    vs

      17    42    >   IS_EQUAL                                         ~30     !0, 1
            43      > JMPZ                                                     ~30, ->52
      18    44    >   IS_EQUAL                                         ~31     !1, 2
            45      > JMPZ                                                     ~31, ->52
      19    46    >   IS_EQUAL                                         ~32     !2, 3
            47      > JMPZ                                                     ~32, ->52
      20    48    >   IS_EQUAL                                         ~33     !3, 4
            49      > JMPZ                                                     ~33, ->52
      21    50    >   IS_EQUAL                                         ~34     !4, 5
            51      > JMPZ                                                     ~34, ->52
    

    Apart of that the evaluation for both implementations looks pretty similar.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口