1

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.

mailmindlin
  • 616
  • 2
  • 7
  • 25
  • 1
    Measure the difference. – jxh Aug 29 '14 at 14:56
  • use the second snippit. it can clearly be done only with registers, while the first snippet might (probably will) involve chache/memory/file – Steve Cox Aug 29 '14 at 14:56
  • 200 times a second? It sounds like you have a maximum of 5 ms to do something with foo. – Elliott Frisch Aug 29 '14 at 14:57
  • how fast your processor is? how complicated the function is? – Jason Hu Aug 29 '14 at 14:59
  • 5
    Writing the variable to the screen will dwarf the difference in time between initializing inside and outside of the loop – Don Shankin Aug 29 '14 at 15:00
  • Hard to guess. Such local variable may not even exist in memory (your compiler will decide what to do according to code). You should measure anyway...you **print to screen** and you try to optimize local stack-allocated variables allocation speed? Hmmmm – Adriano Repetti Aug 29 '14 at 15:00
  • ... I'm not a fan of "implicitly trust the compiler", but this is simple enough that I'd feel comfortable enough to let the compiler optimize for you. – Don Shankin Aug 29 '14 at 15:01
  • @DonShankin [compiler will always do much better](http://stackoverflow.com/a/9601625/1207195) than 99% of us. And in for the other 1% they'll do better at least 90% times... – Adriano Repetti Aug 29 '14 at 15:03
  • @AdrianoRepetti exactly. Things become a little stickier in the embedded world, but this is true. – Don Shankin Aug 29 '14 at 15:08
  • @DonShankin The "dirty hands world"! ;) – Adriano Repetti Aug 29 '14 at 15:09
  • @AdrianoRepetti You spend 99% of your time in 3% of your code though. – Steve Cox Aug 29 '14 at 15:14
  • Normally local variables reuses the same stack space – David Ranieri Aug 29 '14 at 15:22
  • Would the cumulative overhead of repeated local variable initialization/allocation eventually overtake the cumulative overhead of the single global declaration? – taz Aug 29 '14 at 15:24
  • @taz You really can't compare them that way. The cost of a global variable is in terms of global storage space and potentially making the code harder to understand/reason about. A local variable takes stack space and execution time if it has to be repeatedly re-initialized. – nobody Aug 29 '14 at 15:28
  • Is is so hard to measure it yourself? It would take like 1 minute to write the code. – michaelmeyer Aug 29 '14 at 15:33
  • @SteveCox pretty right but it may worth or not to spend time to optimize that 3%. In this case printing to screen is much much slower than anything else but also for "fast" code such optimization is hardly to care about. Function call itself is many times slower, cache (if any) will impact much more, a jump hurts ten times more, an optimized code path will give much more benefits and so on... – Adriano Repetti Aug 29 '14 at 15:33
  • @doukremt I think it's hard to measure this in a reliable way anyway local variables are also easier to optimize for compilers (well in reality I think compiler will understand global variable usage and generated code won't be different). – Adriano Repetti Aug 29 '14 at 15:35
  • A single pipeline stall will wipe out your micro-optimization here. Smells like premature optimization to me. – Elliott Frisch Aug 29 '14 at 15:51
  • Does `bar` need to be re-entrant? If so, using a global/static variable won't work. How is the value of `foo` set? Do you call another function within `bar` to get the new value for `foo`? Is it based on an argument to `bar`? Is it possible you could eliminate the need for `foo` entirely? – John Bode Aug 29 '14 at 15:56

2 Answers2

1

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.

Steve Cox
  • 1,947
  • 13
  • 13
Alex Kleiman
  • 709
  • 5
  • 14
  • 1
    Also, just use common sense/good judgement. As @DonShankin points out in the comments, the display I/O that is also being performed is likely to take orders of magnitude longer than initializing a local variable - so don't worry about it. *If* the function turns out to be too slow, *then* investigate/profile. – nobody Aug 29 '14 at 15:31
0

Usually, allocation of automatic variables is a noop in C: all local variables are allocated with a single instruction that decrements the stack pointer, something that needs to be done anyway in almost all functions. And, usually, accesses to stack memory are the fastest you can get. So, in general, you should expect no performance difference between your two programs. If there is a difference, you should expect the global variable to be a tad slower.

However, I used the words "usually" and "in general" on purpose: Hardware may exist, where this is not the case. And I do not know about your processor. So, to be sure, heed Alex Kleiman's advice and measure. Measurement is the only source of truth when it comes to optimization.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106