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 ... */
}