4

Is there a performance issue using static variables in function calculations, does it affect speed of function execution, as static variables are initialized only once?

Question is for a highly repetetive calling optimizations.

Consider this example

int calcme(int a, int b)
{
 static int iamstatic = 20;
 return a*iamstatic + b;
}

The reason to use static is to hope, that iamstatic will not be put on stack every time a function is called and it is designed to change if needed. (Static variable change code is ommited)

Ulterior
  • 2,786
  • 3
  • 30
  • 58

8 Answers8

8

To my opinion you might reduce performance. When you use static, the memory is located at the bss part for the program. When the function is called it a access two different locations, the function memory and the parameter memory. If it is local then you may gain performance due to localization, when all parameters are at the same cache line, that is when the cpu read memory it reads a full cache line (16 bytes is a common size of line), you read all the data in one memory access into the cache.

roni bar yanai
  • 1,492
  • 10
  • 12
3

Yes. In your example, it probably won't help (at least enough to care about). In fact, it might even hurt a bit, because it's likely to involve loading the data from memory where a local might just involve loading a value into a register. If the initialization is slow enough for the performance to matter, making the variable static can improve performance as long as only being initialized once is acceptable.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • So basically the performance would be the same as having it in global scope? – Ulterior Jul 09 '11 at 17:35
  • @user757808: Yes. Either way, the variable has static storage duration. Putting it into a function just restricts the visibility of the name to that function. That basically only affects things at compile time (looking up names), not run time. – Jerry Coffin Jul 09 '11 at 17:37
2

The answer is technically "no" because the C standard doesn't specify it.

Efficiency depends on many things like variable usage and hardware. If you wanted, you could write variant functions that are called many, many times to test the difference. In practice, there is probably a very small and insignificant difference.

BlackJack
  • 2,826
  • 1
  • 22
  • 25
1

This is the most readable and highest performing version of the function.

int calcme(int a, int b)
{
    return a*20 + b;
}

If you put a constant in a static variable, maybe the compiler will figure out that it's never changed, and convert it to an immediate operand. Maybe the compiler won't figure it out and it will load the static from memory.

If you put a constant in a global variable, the compiler WILL load the variable from memory.

You are trying to outsmart the optimizer, and this is almost always a bad idea.

Here is your original code, compiled:

    leal    (%rdi,%rdi,4), %edi
    leal    (%rsi,%rdi,4), %eax
    ret

This is the same code generated by return a*20 + b;, but your code is more difficult to read. Note that the static variable doesn't actually get stored anywhere, it gets converted to an immediate operand (and then strength reduction reduces it even further). You'll get the same exact performance from the following code:

int calcme(int a, int b)
{
    int local = 20; // "initialized" every time the function is called
                    // yet the assembly is the same
    return a*local + b;
}
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • @user757808: It **HAS** to be constant, because there is no way to change a static variable except from inside the function. The function does not do that. Therefore it is constant. – Dietrich Epp Jul 09 '11 at 18:02
0

I don't immediately see any performance gain or penalty using static vars.

What you could do is to disassemble two identical functions and compare the code, or profile each.

Vinicius Kamakura
  • 7,665
  • 1
  • 29
  • 43
0

Why don't you do a simple

#define MY_CONSTANT 20

/* ... */

return a*MY_CONSTANT + b;

Maybe the compiler knows a trick to multiply by 20 (which it won't be able to apply when you multiply by iamstatic).

pmg
  • 106,608
  • 13
  • 126
  • 198
0

If you compile with optimization, it depends on situation. In your example, iamstatic is constant, I guess speed will not change.

You should try to measure execution time or look into assembly code.

kamae
  • 1,825
  • 1
  • 16
  • 21
0

The accepted answer is correct, but perhaps more importantly, you should never choose the storage duration of a variable based on trying to make it "faster", but instead based on the correct semantics for the code.

  1. If the variable is storing global state that needs to persist across calls to the function, it should have static storage duration. (Usually this is harmful but sometimes it's a necessary evil or at least makes things sufficiently easier that you're willing to commit the offense of writing bad code to save a lot of development time/cost.)
  2. If a variable is storing data that's specific to the current invocation of the function, it should have automatic storage duration (the default).
  3. If data belongs to the current execution context but needs to persist after the function returns, it should be stored in an object obtained by malloc.
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711