Today my colleague from work said that access to global variables on microcontrollers consumes more power than access to local variables, unfortunately she did not explain that. At the beginning, values of global varaibles are copied to RAM and then we operate on values from RAM, so how does it work that they consume more power? Are local variables stored in CPU registers, not in RAM ? We are programming in C language using GCC.
-
\$\begingroup\$ Why don't you ask her? \$\endgroup\$Leon Heller– Leon Heller2015年02月19日 16:12:00 +00:00Commented Feb 19, 2015 at 16:12
-
3\$\begingroup\$ What sort of microcontroller are you talking about? Perhaps the distinction is not between "global variable" and "local variable", which is a software concept, but rather between "internal memory" and "external memory", which could have power consumption implications. \$\endgroup\$Dave Tweed– Dave Tweed2015年02月19日 16:12:13 +00:00Commented Feb 19, 2015 at 16:12
-
2\$\begingroup\$ There might be some specific context in which accessing of global variables takes more power than accessing local variables, but I think I could also construct a context in which the reverse is true. So as a sweeping statement I would consider it nonsense. \$\endgroup\$Wouter van Ooijen– Wouter van Ooijen2015年02月19日 16:13:05 +00:00Commented Feb 19, 2015 at 16:13
-
\$\begingroup\$ I asked her but she was not able to explain that and it is MSP430. \$\endgroup\$Al Bundy– Al Bundy2015年02月19日 16:15:00 +00:00Commented Feb 19, 2015 at 16:15
-
2\$\begingroup\$ I can more easily imagine a power consumption difference between statically allocated memory and dynamically allocated, or register vs. non-register. If the compiler emits code that takes more cycles to execute, generally it will consume more power for the same operation. \$\endgroup\$Spehro 'speff' Pefhany– Spehro 'speff' Pefhany2015年02月19日 16:25:35 +00:00Commented Feb 19, 2015 at 16:25
1 Answer 1
I suppose, that is what she had in mind. Variables stored in registers would need less power to access as there is no bus, address decoding and all that stuff you need for RAM access needed.
Global variables will most likely always be stored in RAM if you don't do some crazy stuff with your compiler (allocating a register for a variable).
But you can't be sure that local variables will not be stored in RAM. Your controller has only a very limited amount of registers available, some of them will be needed to perform other operations. If you need more local variables, you will end up on the RAM with them as well.
On calling a function, stuff gets pushed on the stack which is also RAM. Most of the code is stored in flash and reading from there consumes a lot more power than from RAM.
So if this was an argument to make you stop use global variables I'd say it's a rather weak one.
I actually found this (because of the hint of MSP430 in the comments):
ULP Advisor > Rule 7.1 Use local instead of global variables where possible
C compilers for micro-controllers in general and for MSP430 MCUs specifically allocate the variables declared in the global scope into the RAM location of the memory structure. On the other hand, local variables are first allocated into CPU registers (until the function runs out of the CPU registers for its local variables). Accesses to CPU registers are the most efficient and quickest, when compared to accesses to memory locations in RAM or even worse, in FLASH. Therefore, any global variables that are only accessed within one function can be safely relocated into the function local scope to optimize the memory access speed and power consumption.
So yes, she was probably thinking about this. But I wonder how much savings you can actually get.
-
4\$\begingroup\$ Reading further in the link, this notification was due to a global only being used in a single function. In this case it is a matter of good programming practice to limit scope. So, the preference of locals to globals is reasonable, but the power savings justification is a bit silly. Consider that in most cases you can achieve much better power savings by using inline assembly or better use of all those low power modes that the msp430 is known for. \$\endgroup\$caveman– caveman2015年02月19日 17:29:26 +00:00Commented Feb 19, 2015 at 17:29