Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Return Performance

Tags:

c++

I have a question about performance. I think this can also applies to other languages (not only C++).

Imagine that I have this function:

int addNumber(int a, int b){
    int result = a + b;
    return result;
}

Is there any performance improvement if I write the code above like this?

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

I have this question because the second function doesn´t declare a 3rd variable. But would the compiler detect this in the first code?

like image 434
Daniel Oliveira Avatar asked Sep 25 '26 19:09

Daniel Oliveira


2 Answers

To answer this question you can look at the generated assembler code. With -O2, x86-64 gcc 6.2 generates exactly the same code for both methods:

addNumber(int, int):
    lea     eax, [rdi+rsi]
    ret
addNumber2(int, int):
    lea     eax, [rdi+rsi]
    ret

Only without optimization turned on, there is a difference:

addNumber(int, int):
    push    rbp
    mov     rbp, rsp
    mov     DWORD PTR [rbp-20], edi
    mov     DWORD PTR [rbp-24], esi
    mov     edx, DWORD PTR [rbp-20]
    mov     eax, DWORD PTR [rbp-24]
    add     eax, edx
    mov     DWORD PTR [rbp-4], eax
    mov     eax, DWORD PTR [rbp-4]
    pop     rbp
    ret
addNumber2(int, int):
    push    rbp
    mov     rbp, rsp
    mov     DWORD PTR [rbp-4], edi
    mov     DWORD PTR [rbp-8], esi
    mov     edx, DWORD PTR [rbp-4]
    mov     eax, DWORD PTR [rbp-8]
    add     eax, edx
    pop     rbp
    ret

However, performance comparison without optimization is meaningless

like image 110
463035818_is_not_a_number Avatar answered Sep 27 '26 11:09

463035818_is_not_a_number


In principle there is no difference between the two approaches. The majority of compilers have handled this type of optimisation for some decades.

Additionally, if the function can be inlined (e.g. its definition is visible to the compiler when compiling code that uses such a function) the majority of compilers will eliminate the function altogether, and simply emit code to add the two variables passed and store the result as required by the caller.

Obviously, the comments above assume compiling with a relevant optimisation setting (e.g. not doing a debug build without optimisation).

Personally, I would not write such a function anyway. It is easier, in the caller, to write c = a + b instead of c = addNumber(a, b), so having a function like that offers no benefit to either programmer (effort to understand) or program (performance, etc). You might as well write comments that give no useful information.

 c = a + b;    //   add a and b and store into c

Any self-respecting code reviewer would complain bitterly about uninformative functions or uninformative comments.

I'd only use such a function if its name conveyed some special meaning (i.e. more than just adding two values) for the application

 c = FunkyOperation(a,b);

 int FunkyOperation(int a, int b)
 {
    /*  Many useful ways of implementing this operation.
           One of those ways happens to be addition, but we need to
           go through 25 pages of obscure mathematical proof to
           realise that
    */

    return a + b;
 }
like image 42
Peter Avatar answered Sep 27 '26 12:09

Peter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!