Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do temp variables slow down my program?

Suppose I have the following C code:

int i = 5; int j = 10; int result = i + j; 

If I'm looping over this many times, would it be faster to use int result = 5 + 10? I often create temporary variables to make my code more readable, for example, if the two variables were obtained from some array using some long expression to calculate the indices. Is this bad performance-wise in C? What about other languages?

like image 547
Elliot Gorokhovsky Avatar asked Nov 15 '14 19:11

Elliot Gorokhovsky


People also ask

Do global variables slow down program?

Global variables are really slow, in addition to all the other reasons not to use them.

What is the purpose of temporary variables?

In computer programming, a temporary variable is a variable with short lifetime, usually to hold data that will soon be discarded, or before it can be placed at a more permanent memory location. Because it is short-lived, it is usually declared as a local variable, i.e., a variable with local scope.

Are global variables slower C++?

In this sense, a global variable could be slower. conclusion there are performance improve when the global variable is in register. There is no a "global" or "local" variable problem. The performance depends on the access to the variable.

What type of variable is temporary?

A temporary variable exists only within a formula. Its scope is that formula and it has no attributes other than the ones assigned to it within the formula. The variable takes the type of the value on the righthand side of the equation. This value can be any of the field types or boolean.


2 Answers

A modern optimizing compiler should optimize those variables away, for example if we use the following example in godbolt with gcc using the -std=c99 -O3 flags (see it live):

#include <stdio.h>  void func() {   int i = 5;   int j = 10;   int result = i + j;    printf( "%d\n", result ) ; } 

it will result in the following assembly:

movl    $15, %esi 

for the calculation of i + j, this is form of constant propagation.

Note, I added the printf so that we have a side effect, otherwise func would have been optimized away to:

func:   rep ret 

These optimizations are allowed under the as-if rule, which only requires the compiler to emulate the observable behavior of a program. This is covered in the draft C99 standard section 5.1.2.3 Program execution which says:

In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).

Also see: Optimizing C++ Code : Constant-Folding

like image 109
Shafik Yaghmour Avatar answered Nov 09 '22 15:11

Shafik Yaghmour


This is an easy task to optimize for an optimizing compiler. It will delete all variables and replace result with 15.

Constant folding in SSA form is pretty much the most basic optimization there is.

like image 35
usr Avatar answered Nov 09 '22 15:11

usr