I have a function that needs quite some internal temporary storage for its computations (some matrix operations) and I know that this function will be called frequently (say every millisecond throughout the runtime of the program). My gut feeling tells me that it's better to declare those temporary variables static, so there's not so much administrative effort for creating them again and again with each call of the function. I have to initialized them anyway every time the function is called, so keeping them alive is not needed for functional purposes. I am aware that making them static breaks thread-safety, but this is not an issue here.
As knowledge is usually better than any gut feeling, I'd like to know what the "correct" way of handling this case is.
void frequentlyCalledFunction(void)
{
double matrix1[10][10];
double matrix2[10][10];
/* do something with the matrices ... */
}
or
void frequentlyCalledFunction(void)
{
static double matrix1[10][10];
static double matrix2[10][10];
/* do something with the matrices ... */
}
The primary advantage of static local variables over stack-dynamic local variables is that they are slightly more efficient—they require no run-time overhead for allocation and deallocation. Also, if accessed directly, these accesses are obviously more efficient.
Defining a variable static in a method only means that the variable is not "released", i.e. it will keep its value on subsequent calls. It could lead to performance improvement depending on the algorithm, but is certainly not not a performance improvement by itself.
My logic behind this is that, because static variables are persistent between function calls, they are allocated only the first time, and thus, every subsequent call will not allocate memory for them and will become faster, because the memory allocation step is not done.
Static variables are used to keep track of information that relates logically to an entire class, as opposed to information that varies from instance to instance.
There's no difference. There's no code needed to "create" an uninitialised array.
In the case of a static array, the memory is reserved and available all the time. In the case of an automatic array, it's on the stack, and all that's required to "create" it is to move the stack pointer, which is going to happen anyway on entry to the function.
(And one day you'll try to use that function in a multithreaded program, and the static version will suffer occasional intermittent failures that drive you to drink and drugs. It's just not worth the risk.)
I would definitely write it the simplest and most readable way first. Once per millisecond sounds like a very rarely run function to be micro-optimising.
Once you've got it working, benchmark it. Decide if performance is good enough. If it's not, optimise and benchmark again. Don't bend clean code out of shape without very solid numbers to back up your decision.
As usual, you should be profiling first. Local variables probably only cause the stack pointer to be decremented a bit more which should not have any performance penalty.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With