Ulterior
Ulterior

Reputation: 2912

static variables in functions

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)

Upvotes: 4

Views: 788

Answers (8)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215547

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.

Upvotes: 0

kamae
kamae

Reputation: 1867

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.

Upvotes: 0

Dietrich Epp
Dietrich Epp

Reputation: 213748

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;
}

Upvotes: 1

pmg
pmg

Reputation: 108988

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).

Upvotes: 0

roni bar yanai
roni bar yanai

Reputation: 1492

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.

Upvotes: 8

BlackJack
BlackJack

Reputation: 2876

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.

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490623

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.

Upvotes: 3

Vinicius Kamakura
Vinicius Kamakura

Reputation: 7778

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.

Upvotes: 0

Related Questions