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.

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

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮