Why does n++ execute faster than n=n+1?

前端 未结 10 1025
[愿得一人]
[愿得一人] 2021-01-30 22:05

In C language, Why does n++ execute faster than n=n+1?

(int n=...;  n++;)
(int n=...;  n=n+1;)

Our instructor asked

10条回答
  •  情话喂你
    2021-01-30 22:26

    On GCC 4.4.3 for x86, with or without optimizations, they compile to the exact same assembly code, and thus take the same amount of time to execute. As you can see in the assembly, GCC simply converts n++ into n=n+1, then optimizes it into the one-instruction add (in the -O2).

    Your instructor's suggestion that n++ is faster only applies to very old, non-optimizing compilers, which were not smart enough to select the in-place update instructions for n = n + 1. These compilers have been obsolete in the PC world for years, but may still be found for weird proprietary embedded platforms.

    C code:

    int n;
    
    void nplusplus() {
        n++;
    }
    
    void nplusone() {
        n = n + 1;
    }
    

    Output assembly (no optimizations):

        .file   "test.c"
        .comm   n,4,4
        .text
    .globl nplusplus
        .type   nplusplus, @function
    nplusplus:
        pushl   %ebp
        movl    %esp, %ebp
        movl    n, %eax
        addl    $1, %eax
        movl    %eax, n
        popl    %ebp
        ret
        .size   nplusplus, .-nplusplus
    .globl nplusone
        .type   nplusone, @function
    nplusone:
        pushl   %ebp
        movl    %esp, %ebp
        movl    n, %eax
        addl    $1, %eax
        movl    %eax, n
        popl    %ebp
        ret
        .size   nplusone, .-nplusone
        .ident  "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
        .section    .note.GNU-stack,"",@progbits
    

    Output assembly (with -O2 optimizations):

        .file   "test.c"
        .text
        .p2align 4,,15
    .globl nplusplus
        .type   nplusplus, @function
    nplusplus:
        pushl   %ebp
        movl    %esp, %ebp
        addl    $1, n
        popl    %ebp
        ret
        .size   nplusplus, .-nplusplus
        .p2align 4,,15
    .globl nplusone
        .type   nplusone, @function
    nplusone:
        pushl   %ebp
        movl    %esp, %ebp
        addl    $1, n
        popl    %ebp
        ret
        .size   nplusone, .-nplusone
        .comm   n,4,4
        .ident  "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
        .section    .note.GNU-stack,"",@progbits
    

提交回复
热议问题