Four Easy Ways to Drain the Battery

[originally published 7/2/2017]

Image for post
Image for post
CPU usage with an ad blocker and without…

Getting good power performance is tricky business. Writing about that would be very hard. So instead I’m going tell you how to get bad energy economy. I feel confident I can get that right. And the information might even be helpful.

1. Set lots of timers

If you can’t run your code flat out all the time, the next best thing is to be sure there’s a nice high frequency timer or seven that’s waking up your code and making it do something. The more unimportant that something is, the better you are at wasting power. Asking the same question over, and, over again like: “Is my ad on this screen now? How about now? Is it on now? How about now?” is fantastic way to do this. But to really get the gold medal for wasted energy, you need to ask a question that has an event you could have subscribed to instead.

2. Use long running animations

This is kind of like timers only better, you don’t even have to use a clumsy/annoying timer API! With just use a few CSS styles or something, you can create a nice 30s animation. Now like #1, there’s levels… so a 30s progress bar that’s animated at 60fps that’s gold medal material there, because we all need to see 60fps fidelity on progress bars. I’ve seen cases where more energy was going into the progress bar than the actual work (which was asynchronous). That’s some serious mojo!

This is not to say that all animations are bad but people do like to try to draw attention to places with animations. If you need an animation to draw attention probably the real problem is the thing is a bad feature in the first place. On the other hand nothing makes me wiggle like the hearts animation in Messenger, that’s pure joy and worth every Joule.

Animations could be the best bang/buck in power wastage because it can be very little code, it can burn the GPU as well as the CPU, and, with a little effort, you could implicate all kinds of nice layout and measurement code you didn’t even have to write!

3. Avoid Work Alignment

If you’re trying really hard to waste power on your timers but you just don’t have enough timers or other events to really suck that power down then you can get the most out of the work you do have by making sure that none of it fires at the same time. A 1hz timer is maybe not so bad, but if you can get 10 of them to stagger why that’s nearly as good as a 10hz timer — a much better tool for sucking down power. Don’t let that CPU get into an annoying low-power state, poke it in the nose here and there.

4. Do lots of speculative work

Everyone enjoys a good preload. If your CPU might get to be idle, be sure to guess what the user is going to do next. Don’t worry if it’s right or wrong or how much energy it takes, just keep that CPU moving. CPUs are like that shark in Jaws, if it stops moving it’ll die. So be sure to throw in some speculative work if there isn’t any real work. Or even if there is real work! Remember: you’ll get much poorer performance in your actual work if you keep your processor busy on speculation. If your speculative guesses are wrong half the time that’s a nice 50% waste of power right there! You’re well on your way to draining the battery like a champ.

Summary

Hopefully these four methods will inspire you to think of other ways to waste energy.

If you decide you need to stop wasting energy, then remember this mantra, which is actually pretty good in a lot of dimensions. The three steps are:

“Do less. Do it once. Then stop.”

Written by

I’m a software engineer at Facebook; I specialize in software performance engineering and programming tools generally. I survived Microsoft from 1988 to 2017.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store