Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocating a temporary variable or compute the expression twice [duplicate]

Tags:

c++

c

I am trying to write a code as efficient as possible and I encountered the following situation:

int foo(int a, int b, int c) 
{
    return (a + b) % c; 
}

All good! But what if I want to check if the result of the expression to be different of a constant lets say myConst. Lets say I can afford a temporary variable.

What method is the fastest of the following:

int foo(int a, int b, int c) 
{
    return (((a + b) % c) != myConst) ? (a + b) % c : myException; 
} 

or

int foo(int a, int b, int c) 
{
    int tmp = (a + b) % c
    return (tmp != myConst) ? tmp : myException; 
}

I can't decide. Where is the 'line' where recalculation is more expensive than allocating and deallocating a temporary variable or the other way around.

like image 296
Tandura Avatar asked May 04 '16 18:05

Tandura


People also ask

How do you create a temporary variable?

Temp Variables are created using a “DECLARE” statement and are assigned values using either a SET or SELECT command. After declaration, all variables are initialized as NULL, unless a value is provided as part of the declaration. This acts like a variable and exists for a specific batch of query execution.

How do you assign a variable in C++?

Declaring (Creating) Variablestype variableName = value; Where type is one of C++ types (such as int ), and variableName is the name of the variable (such as x or myName). The equal sign is used to assign values to the variable.

Where is temporary variable stored?

Digital systems often use a number of registers to store temporary variables. This group of registers, called a register file, is usually built as a small, multiported SRAM array because it is more compact than an array of flip-flops.

Can a variable be executed without being assigned a value?

A variable must be assigned a value before using it, otherwise, C# will give a compile-time error. The value of a variable can be changed anytime after initializing it. Multiple variables of the same data type can be declared and initialized in a single line separated by commas.


2 Answers

Don't worry about it, write concise code and leave micro-optimizations to the compiler.

In your example writing the same calculation twice is error prone - so do not do this. In your specific example, compiler is more than likely to avoid creating a temporary on the stack at all!

Your example can (does on my compiler) produce following assembly (i have replaced myConst with constexpr 42 and myException with 0):

foo(int, int, int):
        leal    (%rdi,%rsi), %eax       # this adds a and b, puts result to eax
        movl    %edx, %ecx      # loads c
        cltd
        idivl   %ecx  # performs division, puts result into edx
        movl    $0, %eax        #, prepares to return exception value
        cmpl    $42, %edx       #, compares result of division with magic const
        cmovne  %edx, %eax  # overwrites pessimized exception if all is cool
        ret

As you see, there is no temporary anywhere in sight!

like image 63
SergeyA Avatar answered Oct 09 '22 21:10

SergeyA


Use the later.

  1. You're not computing the same value twice.
  2. The code is more clear.
  3. Creating local variables on the stack doesn't take any significant amount of time.
like image 40
dbush Avatar answered Oct 09 '22 19:10

dbush