I was wondering if there are some rules or hints how to prevent battery drain directly while coding. I know that there may be some ways to code to minimize workload on the processor or prevent leaks. However, does anybody have something like a guide or a something like a "checklist" besides the usual suspects like location service and internet connection?
-
I've written some stuff on this. I'll dig it up and post. – Taylor Kidd Feb 24 '15 at 18:34
-
1Please clarify what you want. You say, "prevent battery drain directly while coding," seems to say that you want to minimize drain while compiling or debugging your code on your device. "Ways to code," implies that you want to know about minimizing the battery drain by the app that you are coding/creating. Which are you asking? – Taylor Kidd Feb 24 '15 at 18:44
-
Seems clear that OP means the latter. – Ben Zotto Feb 24 '15 at 20:26
-
This question is fairly broad, and hard to answer in this form. (The answer is "anything that runs the CPU or GPU hard, uses the radios, or tracks GPS closely will affect your battery life"). I'd suggest you edit to discuss what exactly your app is doing, and what services it's using. That will result in more pragmatic advice. – Ben Zotto Feb 24 '15 at 20:42
-
Thanks for the comments. However, it is not actually for one or "my" app. It´s meant to be general. Since there are a lot of guides in "how to prevent memory leaks" I hoped to find something that gives developers the possibility to consider also Energy Consumption besides the usual suspects (4G/wifi/Bluethooth/memory), considering software-architecture or the general concept of an app. @Taylor Kidds answer is a very good start regarding CPU usage. :) – Corona Feb 27 '15 at 11:50
-
1This may help you. https://software.intel.com/en-us/energy-efficient-software – Taylor Kidd Feb 27 '15 at 17:44
1 Answers
I'll assume you mean your application. In my experience, the major consumers of energy are, where #1 is most important:
- CPU usage
- 4G
- WiFi
- Bluetooth
- Memory
Whether 4G or WiFi is worse depends upon your usage, e.g. whether you are talking with a poor signal over your cellular network or are streaming video over your WiFi. GPS depends upon if you are using it. If you are getting turn by turn directions, it'll turn your phone into a little heater and drain your battery very quickly.
Minimizing 4G, WiFi, and Bluetooth usage is pretty straight forward. I not sure it's possible to reduce the energy used by memory in any practical way.
CPU usage is the greatest potential energy hog because it can continuously suck down electrons all of the time. Thankfully, modern processors shut down when doing nothing, i.e. idling. This is called dropping into an idle / C-state. As you can guess, a cell phone is doing nothing pretty much most of the time.
There are ways you can write your program to minimize CPU energy usage. Actually, a better way of saying this is that there are ways you can defeat these energy saving features by writing your program wrong. If the CPU goes to sleep to minimize power then waking it up increases energy usage. Another factor to consider is how long the CPU is asleep. The longer you can leave the processor idle, the deeper a sleep state it can enter, and deeper sleep states use less power.
So what do you need to do to minimize your CPU usage? You want to use the CPU less, or to say it another way, have your program get done whatever it’s doing faster. Also, increase the length of time that your program is idle.
Now let's take a look at some concrete things you can do:
Have your program do whatever it's doing as fast as possible. Do this by finding the fastest algorithm and implementing it in the most efficient way possible. To say it another way, optimize.
Minimize checking on events. The more you check on whether an event occurred, the more you wake up the processor, the less likely it can drop into a really deep sleep state. Do this by figuring out the maximum interval that you have to check for some event while still maintaining performance.

- 1,463
- 1
- 9
- 11
-
Wow thank you for having that time. Actually I was already aware of 4G/WiFi or Loops. And the CPU usage was exactly what I meant with "while coding". In the middle of decision like wether using a Loop or switch/case (example) I would like to consider also Energy consumption for my decisions. Also in cases of "code redesign" when improving memory usage I would also like to care for the Battery. But your hints are a great start. Thanks a lot :) – Corona Feb 27 '15 at 11:20