-
Static vars
Say I have a function runs a coupla 1000 times.
Would it increase the speed if i declared the variables in that function as static and simply re-initialized their values myself every time?
I mean, it would avoid having to allocate/deallocate the variables every time the function runs... or perhaps I have the wrong idea of how static variables work??
-
First thing to bear in mind is that local variables might be completely optimised out of existence (and placed in registers) by the compiler. This cannot happen if you make your variables static.
Also, unless your function is really simple (compiler dependent), you are going to get a stack frame, and its usually one instuction to set this up, no matter the number of local vars.