<p>In a typical handheld/portable embedded system device Battery life is a major concern in design of H/W, S/W and the features the device can support. From the Software programming perspective, one is aware of MIPS, Memory(Data and Program) optimized code.
I am aware of the H/W Deep sleep mode, Standby mode that are used to clock the hardware at lower Cycles or turn of the clock entirel to some unused circutis to save power, but i am looking for some ideas from that point of view:</p>

<p>Wherein my code is running and it needs to keep executing, given this how  can I write the code "power" efficiently so as to consume minimum watts?</p>

<p>Are there any special programming constructs, data structures, control structures which i should look at to achieve minimum power consumption for a given functionality.</p>

<p>Are there any s/w high level design considerations which one should keep in mind at time of code structure design, or during low level design to make the code as power efficient(Least power consuming) as possible? </p>

[{'answer_id': 61889, 'author': '1800 INFORMATION', 'author_id': 3146, 'author_profile': 'https://Stackoverflow.com/users/3146', 'pm_score': 3, 'selected': False, 'text': '<p>Do not poll. Use events and other OS primitives to wait for notifiable occurrences. Polling ensures that the CPU will stay active and use more battery life.</p>\n'}, {'answer_id': 61903, 'author': 'Omer van Kloeten', 'author_id': 4979, 'author_profile': 'https://Stackoverflow.com/users/4979', 'pm_score': 1, 'selected': False, 'text': '<p>Consider using the network interfaces the least you can. You might want to gather information and send it out in bursts instead of constantly send it.</p>\n'}, {'answer_id': 61911, 'author': 'Tim Williscroft', 'author_id': 2789, 'author_profile': 'https://Stackoverflow.com/users/2789', 'pm_score': 3, 'selected': False, 'text': '<p>Zeroith, use a fully static machine that can stop when idle. You can\'t beat zero Hz.</p>\n\n<p>First up, switch to a tickless operating system scheduler. Waking up every millisecend or so wastes power.  If you can\'t, consider slowing the scheduler interrupt instead.</p>\n\n<p>Secondly, ensure your idle thread is a power save, wait for next interrupt instruction.\nYou can do this in the sort of under-regulated "userland" most small devices have.</p>\n\n<p>Thirdly, if you have to poll or perform user confidence activities like updating the UI,\nsleep, do it, and get back to sleep.  </p>\n\n<p>Don\'t trust GUI frameworks that you haven\'t checked for "sleep and spin" kind of code.\nEspecially the event timer you may be tempted to use for #2.</p>\n\n<p>Block a thread on read instead of polling with select()/epoll()/ WaitForMultipleObjects().\nPuts stress on the thread scheuler ( and your brain) but the devices generally do okay.\nThis ends up changing your high-level design a bit; it gets tidier!.\nA main loop that polls all the things you Might do ends up slow and wasteful on CPU, but does guarantee performance. ( Guaranteed to be slow)</p>\n\n<p>Cache results, lazily create things. Users expect the device to be slow so don\'t disappoint them. Less running is better. Run as little as you can get away with.\nSeparate threads can be killed off when you stop needing them.</p>\n\n<p>Try to get more memory than you need, then you can insert into more than one hashtable and save ever searching. This is a direct tradeoff if the memory is DRAM.</p>\n\n<p>Look at a realtime-ier system than you think you might need. It saves time (sic) later.\nThey cope better with threading too.</p>\n'}, {'answer_id': 61912, 'author': 'macbirdie', 'author_id': 5049, 'author_profile': 'https://Stackoverflow.com/users/5049', 'pm_score': 5, 'selected': False, 'text': '<ul>\n<li>Like <code>1800 INFORMATION</code> said, avoid polling; subscribe to events and wait for them to happen</li>\n<li>Update window content only when necessary - let the system decide when to redraw it</li>\n<li>When updating window content, ensure your code recreates as little of the invalid region as possible</li>\n<li>With quick code the CPU goes back to deep sleep mode faster and there\'s a better chance that such code stays in L1 cache</li>\n<li>Operate on small data at one time so data stays in caches as well</li>\n<li>Ensure that your application doesn\'t do any unnecessary action when in background</li>\n<li>Make your software not only power efficient, but also power aware - update graphics less often when on battery, disable animations, less hard drive thrashing</li>\n</ul>\n\n<p>And read some other <a href="https://software.intel.com/en-us/articles/partner-energy-efficient-software-guidelines" rel="nofollow noreferrer">guidelines</a>. ;)</p>\n\n<p>Recently a series of posts called <a href="http://software.intel.com/en-us/search/site/field_tags/software-17970/field_tags/acpi-18789/language/en/type/blog?query=Optimizing%20Software%20Applications%20for%20Power" rel="nofollow noreferrer">"Optimizing Software Applications for Power"</a>, started appearing on Intel Software Blogs. May be of some use for x86 developers.</p>\n'}, {'answer_id': 61971, 'author': 'Garry Shutler', 'author_id': 6369, 'author_profile': 'https://Stackoverflow.com/users/6369', 'pm_score': 3, 'selected': False, 'text': "<p>From my work using smart phones, the best way I have found of preserving battery life is to ensure that everything you do not need for your program to function at that specific point is disabled.</p>\n\n<p>For example, only switch Bluetooth on when you need it, similarly the phone capabilities, turn the screen brightness down when it isn't needed, turn the volume down, etc.</p>\n\n<p>The power used by these functions will generally far outweigh the power used by your code.</p>\n"}, {'answer_id': 62073, 'author': 'Ned', 'author_id': 1105, 'author_profile': 'https://Stackoverflow.com/users/1105', 'pm_score': 1, 'selected': False, 'text': '<p>Look at what your compiler generates, particularly for hot areas of code.</p>\n'}, {'answer_id': 62107, 'author': 'itj', 'author_id': 888, 'author_profile': 'https://Stackoverflow.com/users/888', 'pm_score': 1, 'selected': False, 'text': "<p>If you have low priority intermittent operations, don't use specific timers to wake up to deal with them, but deal with when processing other events.</p>\n\n<p>Use logic to avoid stupid scenarios where your app might go to sleep for 10 ms and then have to wake up again for the next event.  For the kind of platform mentioned it shouldn't matter if both events are processed at the same time.\nHaving your own timer &amp; callback mechanism <strong>might</strong> be appropriate for this kind of decision making.  The trade off is in code complexity and maintenance vs. likely power savings.</p>\n"}, {'answer_id': 152989, 'author': 'Brian Knoblauch', 'author_id': 15689, 'author_profile': 'https://Stackoverflow.com/users/15689', 'pm_score': 1, 'selected': False, 'text': '<p>Simply put, do as little as possible.</p>\n'}, {'answer_id': 153049, 'author': 'Lorenzo Boccaccia', 'author_id': 2273540, 'author_profile': 'https://Stackoverflow.com/users/2273540', 'pm_score': 0, 'selected': False, 'text': '<p>also something that is not trivial to do is reduce precision of the mathematical operations, go for the smallest dataset available and if available by your development environment pack data and aggregate operations. </p>\n\n<p>knuth books could give you all the variant of specific algorithms you need to save memory or cpu, or going with reduced precision minimizing the rounding errors</p>\n\n<p>also, spent some time checking for all the embedded device api - for example most symbian phones could do audio encoding via a specialized hardware</p>\n'}, {'answer_id': 223863, 'author': 'Toybuilder', 'author_id': 22329, 'author_profile': 'https://Stackoverflow.com/users/22329', 'pm_score': 1, 'selected': False, 'text': '<p>Well, to the extent that your code can execute entirely in the processor cache, you\'ll have less bus activity and save power.  To the extent that your program is small enough to fit code+data entirely in the cache, you get that benefit "for free".  OTOH, if your program is too big, and you can divide your programs into modules that are more or less independent of the other, you might get some power saving by dividing it into separate programs.  (I suppose it\'s also possible to make a toolchain that spreas out related bundles of code and data into cache-sized chunks...)</p>\n\n<p>I suppose that, theoretically, you can save some amount of unnecessary work by reducing the number of pointer dereferencing, and by refactoring your jumps so that the most likely jumps are taken first -- but that\'s not realistic to do as a programmer.  </p>\n\n<p>Transmeta had the idea of letting the machine do some instruction optimization on-the-fly to save power...  But that didn\'t seem to help enough... <a href="http://arstechnica.com/news.ars/post/20080925-transmeta-puts-itself-longrun-technology-up-for-sale.html" rel="nofollow noreferrer">And look where that got them.</a></p>\n'}, {'answer_id': 774264, 'author': 'geschema', 'author_id': 63730, 'author_profile': 'https://Stackoverflow.com/users/63730', 'pm_score': 2, 'selected': False, 'text': "<p>To avoid polling is a good suggestion.</p>\n\n<p>A microprocessor's power consumption is roughly proportional to its clock frequency, and to the square of its supply voltage. If you have the possibility to adjust these from software, that could save some power. Also, turning off the parts of the processor that you don't need (e.g. floating-point unit) may help, but this very much depends on your platform. In any case, you need a way to measure the actual power consumption of your processor, so that you can find out what works and what not. Just like speed optimizations, power optimizations need to be carefully profiled.</p>\n"}, {'answer_id': 875963, 'author': 'old_timer', 'author_id': 16007, 'author_profile': 'https://Stackoverflow.com/users/16007', 'pm_score': 1, 'selected': False, 'text': '<p>Set unused memory or flash to 0xFF not 0x00.  This is certainly true for flash and eeprom, not sure about s or d ram.  For the proms there is an inversion so a 0 is stored as a 1 and takes more energy, a 1 is stored as a zero and takes less.  This is why you read 0xFFs after erasing a block.</p>\n'}, {'answer_id': 1464600, 'author': 'jakobengblom2', 'author_id': 23054, 'author_profile': 'https://Stackoverflow.com/users/23054', 'pm_score': 0, 'selected': False, 'text': '<p>Do your work as quickly as possible, and then go to some idle state waiting for interrupts (or events) to happen.  Try to make the code run out of cache with as little external memory traffic as possible. </p>\n'}, {'answer_id': 1883871, 'author': 'Peter Cordes', 'author_id': 224132, 'author_profile': 'https://Stackoverflow.com/users/224132', 'pm_score': 0, 'selected': False, 'text': '<p>On Linux, install powertop to see how often which piece of software wakes up the CPU.  And follow the various tips that the powertop site links to, some of which are probably applicable to non-Linux, too.</p>\n\n<p><a href="http://www.lesswatts.org/projects/powertop/" rel="nofollow noreferrer">http://www.lesswatts.org/projects/powertop/</a></p>\n'}, {'answer_id': 3594138, 'author': 'hotpaw2', 'author_id': 341750, 'author_profile': 'https://Stackoverflow.com/users/341750', 'pm_score': 0, 'selected': False, 'text': '<p>Choose efficient algorithms that are quick and have small basic blocks and minimal memory accesses.</p>\n\n<p>Understand the cache size and functional units of your processor.</p>\n\n<p>Don\'t access memory.  Don\'t use objects or garbage collection or any other high level constructs if they expands your working code or data set outside the available cache.  If you know the cache size and associativity, lay out the entire working data set you will need in low power mode and fit it all into the dcache (forget some of the "proper" coding practices that scatter the data around in separate objects or data structures if that causes cache trashing).  Same with all the subroutines.  Put your working code set all in one module if necessary to stripe it all in the icache.  If the processor has multiple levels of cache, try to fit in the lowest level of instruction or data cache possible.  Don\'t use floating point unit or any other instructions that may power up any other optional functional units unless you can make a good case that use of these instructions significantly shortens the time that the CPU is out of sleep mode.</p>\n\n<p>etc.</p>\n'}, {'answer_id': 11036453, 'author': 'John U', 'author_id': 1389218, 'author_profile': 'https://Stackoverflow.com/users/1389218', 'pm_score': 1, 'selected': False, 'text': '<p>Rather timely this, article on Hackaday today about measuring power consumption of various commands:\n<a href="http://hackaday.com/2012/06/14/the-effect-of-code-on-power-consumption/" rel="nofollow">Hackaday: the-effect-of-code-on-power-consumption</a></p>\n\n<p>Aside from that:\n<br>- Interrupts are your friends\n<br>- Polling / wait() aren\'t your friends\n<br>- Do as little as possible\n<br>- make your code as small/efficient as possible\n<br>- Turn off as many modules, pins, peripherals as possible in the micro\n<br>- Run as slowly as possible\n<br>- If the micro has settings for pin drive strengh, slew rate, etc. check them &amp; configure them, the defaults are often full power / max speed.\n<br>- returning to the article above, go back and measure the power &amp; see if you can drop it by altering things.</p>\n'}, {'answer_id': 11050925, 'author': 'Martin Thompson', 'author_id': 106092, 'author_profile': 'https://Stackoverflow.com/users/106092', 'pm_score': 0, 'selected': False, 'text': '<p>Don\'t poll, sleep</p>\n\n<p>Avoid using power hungry areas of the chip when possible.  For example multipliers are power hungry, if you can shift and add you can save some Joules (as long as you don\'t do so much shifting and adding that actually the multiplier is a win!)</p>\n\n<p>If you are really serious,l get a power-aware debugger, which can correlate power usage with your source code.  <a href="http://www.iar.com/en/Products/IAR-Embedded-Workbench/Power-debugging/" rel="nofollow">Like this</a></p>\n'}]
['https://Stackoverflow.com/questions/61882', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2759376/']