Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static constant versus constant in a function that is called repeatedly

People also ask

What is the difference between static and constant?

Static is a storage specifier. Const/Constant is a type qualifier. Static can be assigned for reference types and set at run time. Constants are set at compile-time itself and assigned for value types only.

What does static constant mean?

“static const” is basically a combination of static(a storage specifier) and const(a type qualifier). The static determines the lifetime and visibility/accessibility of the variable.

What is a constant static variable?

Static variables are common across all instances of a type. constant variables are specific to each individual instance of a type but their values are known and fixed at compile time and it cannot be changed at runtime. unlike constants, static variable values can be changed at runtime.

What is difference between static and constant variable in C#?

const is a constant value, and cannot be changed. It is compiled into the assembly. static means that it is a value not related to an instance, and it can be changed at run-time (since it isn't readonly ). So if the values are never changed, use consts.


There are a couple of things that affect the answer:

  • First, as long as the value is const it will almost certainly be optimized out in any case. That means that the resulting code will most likely be the same.
  • Second, static members are stored elsewhere which means less locality and probably a cache miss.
  • Third, the cost of initialization depends on the type. In your case, for an int, the cost of initialization is basically nonexistent. For more complex user-defined types, it may be huge.

So the answer is, in cases simple enough for the compiler to figure them out and optimize, it makes zero difference. In your example that would almost certainly be the case.

As long as the variable has a type that is easy and cheap to construct, prefer non-static to avoid the cache miss.

If the type is expensive to construct, you might want to use static.

And of course, last, and most importantly of all:

Don't trust our guesswork. If you are concerned about performance, there is only one correct course of action:

  • Measure it, to verify that it is actually a problem
  • Measure the performance of each possible solution
  • Pick the solution that results in the best performance.

well theoretically the former method is a tiny bit better as it only insantiates the variable once.

However the compiler will simply remove either "const" line and substitute "cout << 12;" into the function (When compiled with optimisations on, obviously).


In first cast ABC will be initialized only once, on first function call. In second case ABC will be initialized every time. You'll feel the difference if ABC is complex type with constructor. It could allocate memory or initialize some mutex. For int there is no difference in practice.

According to C++03 Standard 3.7.1/2:

If an object of static storage duration has initialization or a destructor with side effects, it shall not be eliminated even if it appears to be unused, except that a class object or its copy may be eliminated as specified in 12.8.


It depends on the compiler.

In embedded software, a static const will typically be stored in flash (i.e. code memory), and will be accessed directly, like a normal variable, without any need for initialisation.

In contrast, a non-static const may have its value stored in flash, but the const itself will be created on the stack like a variable, and be initialised just like a variable.

If this is how your compiler handles these scenarios, then the static const is more efficient, as it requires neither stack allocation nor initialisation.

Obviously, these scenarios may be handled different by non-embedded compilers.