Passing arguments to functions with const parameters: is it faster?

前端 未结 4 688
盖世英雄少女心
盖世英雄少女心 2021-02-07 00:32

Consider, for example:

int sum(int a, int b)
{
    return a + b;
}

vs.

int sum(const int a, const int b)
{
    return a + b;
}
         


        
相关标签:
4条回答
  • 2021-02-07 00:49

    The answer probably depends on your compiler, the optimization level, and whether the compiler decides to inline the function. If you are curious about these things, it is easy to just look at the actual assembly produced by your compiler and find out.

    0 讨论(0)
  • 2021-02-07 00:56

    Short answer: No

    Long answer, no, with proof.

    I ran this test, a couple of times, and saw no real time difference, on my MacBook pro compiled with clang:

    int add(int a, int b)
    {
        return a + b;
    }
    
    const int cadd(const int a, const int b)
    {
        return a + b;
    }
    
    int main (int argc, char * argv[])
    {
    #define ITERS 1000000000
    
        clock_t start = clock();
        int j = 0;
        for (int i = 0; i < ITERS; i++)
        {
            j += add(i, i + 1);
        }
    
        printf("add took %li ticks\n", clock() - start);
    
        start = clock();
        j = 0;
        for (int i = 0; i < ITERS; i++)
        {
            j += cadd(i, i + 1);
        }
    
        printf("cadd took %li ticks\n", clock() - start);
    
        return 0;
    }
    

    Output

    add took 4875711 ticks
    cadd took 4885519 ticks
    

    These times really should be taken with a grain of salt, however, as clock isn't the most accurate of timing functions, and can be influenced by other running programs.

    So, here is the compared assembly generated:

    _add:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register %rbp
        movl    %edi, -4(%rbp)
        movl    %esi, -8(%rbp)
        movl    -4(%rbp), %esi
        addl    -8(%rbp), %esi
        movl    %esi, %eax
        popq    %rbp
        ret
    
    _cadd:                                 
        .cfi_startproc    
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register %rbp
        movl    %edi, -4(%rbp)
        movl    %esi, -8(%rbp)
        movl    -4(%rbp), %esi
        addl    -8(%rbp), %esi
        movl    %esi, %eax
        popq    %rb
    

    So, as you can see, there is No difference between the two. Passing an argument as const is only a hint to the caller the the argument will not be changed, and in a simple scenario like the one described above, will not result in any different assembly compiled.

    0 讨论(0)
  • 2021-02-07 01:07

    No. both of them should be same speed. for your reason, assume it passes the original values in to sum function, how about some code out of the sum function modify the original value, for example, another thread.

    In general, the const has no impact the performance at arguments. it do impact the performance if the const is a local/global variable because some calculation can be moved to compiling time as if it is a const.

    0 讨论(0)
  • 2021-02-07 01:07

    Although late to the party, a compiler could put variables defined as const in a read-only memory segment/block, so that if an attempt was made to write to the address, via some pointer tomfoolery, the write to the memory would trigger an exception at runtime.

    -- Jamey

    0 讨论(0)
提交回复
热议问题