• <tfoot id='3cFtE'></tfoot>
    <legend id='3cFtE'><style id='3cFtE'><dir id='3cFtE'><q id='3cFtE'></q></dir></style></legend>
        <bdo id='3cFtE'></bdo><ul id='3cFtE'></ul>

      1. <i id='3cFtE'><tr id='3cFtE'><dt id='3cFtE'><q id='3cFtE'><span id='3cFtE'><b id='3cFtE'><form id='3cFtE'><ins id='3cFtE'></ins><ul id='3cFtE'></ul><sub id='3cFtE'></sub></form><legend id='3cFtE'></legend><bdo id='3cFtE'><pre id='3cFtE'><center id='3cFtE'></center></pre></bdo></b><th id='3cFtE'></th></span></q></dt></tr></i><div id='3cFtE'><tfoot id='3cFtE'></tfoot><dl id='3cFtE'><fieldset id='3cFtE'></fieldset></dl></div>

      2. <small id='3cFtE'></small><noframes id='3cFtE'>

      3. 嵌套 if 语句和“&amp;"操作员

        Nested if statements and quot;amp;amp;quot; operator(嵌套 if 语句和“amp;操作员)
        <tfoot id='Wz6EH'></tfoot>
      4. <legend id='Wz6EH'><style id='Wz6EH'><dir id='Wz6EH'><q id='Wz6EH'></q></dir></style></legend>

        <small id='Wz6EH'></small><noframes id='Wz6EH'>

            <i id='Wz6EH'><tr id='Wz6EH'><dt id='Wz6EH'><q id='Wz6EH'><span id='Wz6EH'><b id='Wz6EH'><form id='Wz6EH'><ins id='Wz6EH'></ins><ul id='Wz6EH'></ul><sub id='Wz6EH'></sub></form><legend id='Wz6EH'></legend><bdo id='Wz6EH'><pre id='Wz6EH'><center id='Wz6EH'></center></pre></bdo></b><th id='Wz6EH'></th></span></q></dt></tr></i><div id='Wz6EH'><tfoot id='Wz6EH'></tfoot><dl id='Wz6EH'><fieldset id='Wz6EH'></fieldset></dl></div>
                <tbody id='Wz6EH'></tbody>

                • <bdo id='Wz6EH'></bdo><ul id='Wz6EH'></ul>

                • 本文介绍了嵌套 if 语句和“&amp;"操作员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  if(a() && b() && c() && d())
                     doSomething();
                  
                  
                  if(a())
                     if(b()) 
                        if(c())
                           if(d())
                              doSomething();
                  

                  这两者之间是否存在任何"性能差异?

                  Is there "any" performance difference between these two?

                  例如,在a()变为0的情况下,它会在第一个if语句中继续运行b()、c()和d()吗?或者它会和第二个嵌套的 if 语句一样工作吗?

                  For example, in a situation that a() turns 0, will it keep running b(), c() and d() in the first if statement? Or will it work same as the second nested if statement?

                  推荐答案

                  它们完全相同.

                  要自己测试,请运行 gcc -S test.c(假设这是您放置源代码的位置)并观察 test.s 的内容.

                  To test this yourself, run gcc -S test.c (presuming that this is where you've put your source) and observe the contents of test.s.

                  以下是使用默认选项(用注释注释)在 gcc 4.8.1 中编译nested-if 方法的方式:

                  Here's how the nested-if approach compiles in gcc 4.8.1 with default options (annotated with comments):

                  main:
                  .LFB0:
                      .cfi_startproc
                      pushq   %rbp
                      .cfi_def_cfa_offset 16
                      .cfi_offset 6, -16
                      movq    %rsp, %rbp
                      .cfi_def_cfa_register 6
                      movl    $0, %eax
                      call    A                        # try to call A
                      testl   %eax, %eax               # look at its return value
                      je  .L3                          # short-circuit if it returned 0
                      movl    $0, %eax                 # ...repeat for B, et al.
                      call    B
                      testl   %eax, %eax
                      je  .L3
                      movl    $0, %eax
                      call    C
                      testl   %eax, %eax
                      je  .L3
                      movl    $0, %eax
                      call    D
                      testl   %eax, %eax
                      je  .L3
                      movl    $0, %eax
                      call    doSomething
                  .L3:
                      popq    %rbp
                      .cfi_def_cfa 7, 8
                      ret
                      .cfi_endproc
                  

                  以下是 && 方法的编译方式:

                  Here's how the && approach compiles:

                  main:
                  .LFB0:
                      .cfi_startproc
                      pushq   %rbp
                      .cfi_def_cfa_offset 16
                      .cfi_offset 6, -16
                      movq    %rsp, %rbp
                      .cfi_def_cfa_register 6
                      movl    $0, %eax
                      call    A                           # try to call A
                      testl   %eax, %eax                  # look at its return value
                      je  .L3                             # short-circuit if it returned 0
                      movl    $0, %eax                    # ...repeat for B, et al.
                      call    B
                      testl   %eax, %eax
                      je  .L3
                      movl    $0, %eax
                      call    C
                      testl   %eax, %eax
                      je  .L3
                      movl    $0, %eax
                      call    D
                      testl   %eax, %eax
                      je  .L3
                      movl    $0, %eax
                      call    doSomething
                  .L3:
                      popq    %rbp
                      .cfi_def_cfa 7, 8
                      ret
                      .cfi_endproc
                  

                  这篇关于嵌套 if 语句和“&amp;"操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)

                      <bdo id='0RgHm'></bdo><ul id='0RgHm'></ul>

                        <legend id='0RgHm'><style id='0RgHm'><dir id='0RgHm'><q id='0RgHm'></q></dir></style></legend>

                        <i id='0RgHm'><tr id='0RgHm'><dt id='0RgHm'><q id='0RgHm'><span id='0RgHm'><b id='0RgHm'><form id='0RgHm'><ins id='0RgHm'></ins><ul id='0RgHm'></ul><sub id='0RgHm'></sub></form><legend id='0RgHm'></legend><bdo id='0RgHm'><pre id='0RgHm'><center id='0RgHm'></center></pre></bdo></b><th id='0RgHm'></th></span></q></dt></tr></i><div id='0RgHm'><tfoot id='0RgHm'></tfoot><dl id='0RgHm'><fieldset id='0RgHm'></fieldset></dl></div>
                          <tbody id='0RgHm'></tbody>
                        • <tfoot id='0RgHm'></tfoot>

                          • <small id='0RgHm'></small><noframes id='0RgHm'>