I have a function in my main loop that's called ~200hz. It's running on an NXT, so the processor is pretty slow. In my function, it instantiates a variable, writes it to screen, and ends. Because of the processor speed, I need this function to be as quick as possible, and was wondering if it is faster to declare a variable in global scope and reset it every time the function is called, or instantiate it within the function. For clarification, which example would be faster?
int foo=0;
void bar() {
foo=0;
//do something with foo
}
vs
void bar() {
int foo=0;
//do something with foo
}
Obviously, I would want to use the second snippet in my code, because global variables are considered 'bad', but the NXT processor is really slow.
Whenever you have something like this, your best bet is to simply measure the speed of both options. There isn't really any way to know for sure which would be better without testing them, particularly given that you don't know how your compiler is even compiling your code.