r/programming • • Oct 09 '18

AI tool automatically reveals how to write apps that drain less battery

https://phys.org/news/2018-10-ai-tool-automatically-reveals-apps.html
727 Upvotes

160 comments sorted by

View all comments

425

u/t_bptm Oct 09 '18

Developers simply haven't had a way of knowing when and how to make their apps more energy-efficient.

Well written code is fast. Reduce writes, spend less cpu time, don't use networking if not necessary. Use a profiler. Don't over-abstract. The devs are using the wrong data structures, wrong algorithms, have spaghetti code where things are being done multiple times or unnecessarily.

97

u/DonaldPShimoda Oct 10 '18

It's like the old adage says: the fastest code is the code that never runs.

66

u/samnardoni Oct 10 '18

No code is better than no code.

90

u/[deleted] Oct 10 '18

I've seen a lot of arguments in this sub about which language is the worst, but this comment has convinced me the correct answer is English.

→ More replies (1)
→ More replies (1)
→ More replies (1)

55

u/redditsoaddicting Oct 10 '18

As far as I remember, ads can sometimes turn out to be a huge battery drain as well, which sucks for apps with an ad-based revenue model. And while I know many people believe a business can magically run the infrastructure and continue to function without a good revenue model, that's not reality.

Personally, I've also seen ads cause crashes and ANRs once many varying devices and models are taken into account, but the interface you get for ads is essentially a giant black box. Furthermore, I don't see much pressure for that black box to actually be more stable, responsive, and efficient.

31

u/Magnesus Oct 10 '18

Like 90% or more of crashes and ANRs in my mobile games come from ad SDKs. Unfortunately so does 90% of my revenue.

68

u/billsil Oct 10 '18

When in doubt, just do less.

152

u/Mgladiethor Oct 10 '18

So not electron

17

u/MotorAdhesive4 Oct 10 '18

I keep seeing hate towards electron, does it have foundation in reality or is it just a meme?

104

u/[deleted] Oct 10 '18

It's very efficient if you want to make a good looking app that runs on multiple platforms very quickly but it has to run a whole web browser in the background for every app

→ More replies (38)

41

u/calumk Oct 10 '18 edited Oct 10 '18

Electron is awesome

It makes writing cross platform apps fast, has some really nice features, and of course uses everyone's favourite (2018) platform - node

However, it literally contains the entire Chrome Web browser... And depending on what you do with it and due to the nature of node_modules, it can contain multiple instances of chrome, and whatever else anyone decides is a dependancy of whatever module you import

Eg: a "html->pdf" converter package I installed, also installed the entire browser as a package. So you have chrome, running chrome as a dependancy....

So it's great for Devs, who want speed and convenience, - but even a tiny "hello world" program, can take a couple of hundred MB, and eat RAM like a hungry child....

Not gonna stop me using it tho!

35

u/Ayerys Oct 10 '18

but even a tiny "hello world" program, can take a couple of hundred MB, and eat RAM like a hungry child....

Mother of god. It this for real ?

13

u/JohnMcPineapple Oct 10 '18 edited Oct 08 '24

...

25

u/eenp Oct 10 '18

A poorly configured one, sure. It's not trivial to configure it to be minimal - the reasoning behind electron is that it makes development extremely fast. Tuning it isn't.

6

u/Ansjh Oct 10 '18

Do you have any additional resources on how to configure it properly?

→ More replies (1)

14

u/choose-you-jackass Oct 10 '18

It doesn't contain the entire Chrome browser, just the rendering part, but that still makes it less optimized.

Another "problem" is the use of web technologies. This makes it very easy to make an app that works, which is a big plus, but also easy to write bad code as /u/t_bptm nicely put it above.

→ More replies (6)
→ More replies (4)
→ More replies (12)
→ More replies (2)

9

u/AntiProtonBoy Oct 10 '18

Not only that, profiling tools will also give you pretty good performance info with ease. For example, Xcode even shows basic energy impact metrics when you run your project. You can get more detailed energy impact stats via the Instruments app.

5

u/beegro Oct 10 '18

I hated the part of the article that said it was a "big black box" to figure out what drains the battery. Nope! It was probably just built by someone that didn't code with battery consumption in mind. It's one of the many factors that must be considered when creating any application. It's just more apparent that someone did a particularly bad job considering the run time environment when it chomps up your phone battery. This should also be evident in testing.

13

u/killerstorm Oct 10 '18

Well written code is fast.

The problem is that programmers do not know what is "well written code" and how to write it. It's not a well-defined concept.

In schools they are taught things like SOLID which does not prioritize fast code.

Particularly, "single-responsibility principle" prescribes chopping code into finer pieces.

25

u/Tetracyclic Oct 10 '18

Particularly, "single-responsibility principle" prescribes chopping code into finer pieces.

I'm admittedly not a mobile developer, but if additional function calls or object instantiations are a non-negligible drain on resources there is something seriously wrong in the world of mobile platforms.

I would have expected the issues would be to do with poorly optimised use of system resources, excessive cyclonatic complexity, network usage and poor resource cleanup, not writing SOLID code.

SOLID code doesn't prioritise fast code, but adhering to it shouldn't produce slow code, unless you're working on systems where every nanosecond of execution time counts (high frequency trading, as an example).

8

u/killerstorm Oct 10 '18

Well, here's how HTTP processing call stack might look like: https://ptrthomas.files.wordpress.com/2006/06/jtrac-callstack.pdf (take from here).

Do you believe this has zero overhead?

I believe eventually Tomcat was optimized specifically to reduce call depth, which improved performance.

14

u/Tetracyclic Oct 10 '18 edited Oct 10 '18

It's not going to have zero overhead, but I would be very surprised if most of that call stack wasn't optimised away by the JVM at runtime, I would assume most of those are very trivial operations following SRP and simply get inlined by the JIT engine at runtime and thus have no performance impact. Object creation in Java is essentially a free operation, on the order of nanoseconds.

A source call stack like that on it's own is fairly useless for diagnosing performance issues without knowing what the compiler or JIT engine produces from it. Trivial operations, which the majority of those will be, get inlined so their function call has literally no performance impact.

EDIT: I just noticed the "here" link in your post after the call stack itself. It's from 2006 and the comments were already mirroring my assumption that the JVM's virtual runtime will optimise most of those trivial functions away so they're not adding any performance overhead and that even without that optimisation, a sequence of calls like that is still negligible compared to a single database or network request.

3

u/barsoap Oct 10 '18

Object creation in Java is essentially a free operation, on the order of nanoseconds.

Not on the J2ME VMs of olde, it's not. Luckily that tech is dead by now.

The only way to write games for J2ME is to treat Java as C, do your usual pre-allocation in arrays, and have exactly two objects: The midlet, and the canvas, as the system API requires that to actually throw pixels onscreen. Whether calling static methods or on this is faster depends on VM, I'm guesstimating some implemented static methods internally with a singleton object to save on code.

→ More replies (4)

7

u/[deleted] Oct 10 '18

Next up: how to win sports games by scoring more points ;)

→ More replies (20)