From 13a6087f31a9b1d3e2011d63ce1fbd613a99f3bf Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 19 Feb 2010 15:59:26 -0800 Subject: Update the "Android Performance" documentation. A lot of this documentation isn't even true of the G1, let alone Froyo running on a Nexus One. Distinguish between truth and fiction, clarify where the JIT affects things, and clarify certain confusions (such as the difference between intrinsics and native methods). I still need to include updated performance numbers in the final section. I should also make the benchmark code available so that people don't have to take our word for these things, and so it's easier for them to get an idea of the performance of future devices and builds. (Though hopefully we can update this every release in future.) Anyway, just removing the untruths is a big step forward. --- docs/html/guide/practices/design/performance.jd | 383 +++++++++--------------- 1 file changed, 142 insertions(+), 241 deletions(-) (limited to 'docs/html/guide/practices/design') diff --git a/docs/html/guide/practices/design/performance.jd b/docs/html/guide/practices/design/performance.jd index ec34ac9..ab3b3d3 100644 --- a/docs/html/guide/practices/design/performance.jd +++ b/docs/html/guide/practices/design/performance.jd @@ -1,39 +1,37 @@ page.title=Designing for Performance @jd:body -

An Android application should be fast. Well, it's probably more accurate to -say that it should be efficient. That is, it should execute as -efficiently as possible in the mobile device environment, with its limited -computing power and data storage, smaller screen, and constrained battery life. - -

As you develop your application, keep in mind that, while the application may -perform well enough in your emulator, running on your dual-core development -computer, it will not perform that well when run a mobile device — even -the most powerful mobile device can't match the capabilities of a typical -desktop system. For that reason, you should strive to write efficient code, to -ensure the best possible performance on a variety of mobile devices.

- -

Generally speaking, writing fast or efficient code means keeping memory -allocations to a minimum, writing tight code, and avoiding certain language and -programming idioms that can subtly cripple performance. In object-oriented -terms, most of this work takes place at the method level, on the order of -actual lines of code, loops, and so on.

+

An Android application should be efficient. It will run on a mobile +device with limited computing power and storage, a smaller screen, and +constrained battery life. Battery life is one reason you might want to +optimize your app even if it already seems to run "fast enough". Battery life +is important to users, and Android's battery usage breakdown means users will +know if your app is responsible draining their battery.

+ +

One of the trickiest problems you'll face when optimizing Android apps is +that it's not generally the case that you can say "device X is a factor F +faster/slower than device Y". +This is especially true if one of the devices is the emulator, or one of the +devices has a JIT. If you want to know how your app performs on a given device, +you need to test it on that device. Drawing conclusions from the emulator is +particularly dangerous, as is attempting to compare JIT versus non-JIT +performance: the performance profiles can differ wildly.

This document covers these topics:

@@ -41,43 +39,17 @@ actual lines of code, loops, and so on.

Introduction

-

There are two basic rules for resource-constrained systems:

+

There are two basic rules for writing efficient code:

-

All the tips below follow from these two basic tenets.

- -

Some would argue that much of the advice on this page amounts to "premature -optimization." While it's true that micro-optimizations sometimes make it -harder to develop efficient data structures and algorithms, on embedded -devices like handsets you often simply have no choice. For instance, if you -bring your assumptions about VM performance on desktop machines to Android, -you're quite likely to write code that exhausts system memory. This will bring -your application to a crawl — let alone what it will do to other programs -running on the system!

- -

That's why these guidelines are important. Android's success depends on -the user experience that your applications provide, and that user experience -depends in part on whether your code is responsive and snappy, or slow and -aggravating. Since all our applications will run on the same devices, we're -all in this together, in a way. Think of this document as like the rules of -the road you had to learn when you got your driver's license: things run -smoothly when everybody follows them, but when you don't, you get your car -smashed up.

- -

Before we get down to brass tacks, a brief observation: nearly all issues -described below are valid whether or not the VM features a JIT compiler. If I -have two methods that accomplish the same thing, and the interpreted execution -of foo() is faster than bar(), then the compiled version of foo() will -probably be as fast or faster than compiled bar(). It is unwise to rely on a -compiler to "save" you and make your code fast enough.

-

Optimize Judiciously

-

As you get started thinking about how to design your application, consider +

As you get started thinking about how to design your application, and as +you write it, consider the cautionary points about optimization that Josh Bloch makes in his book Effective Java. Here's "Item 47: Optimize Judiciously", excerpted from the latest edition of the book with permission. Although Josh didn't have @@ -273,8 +245,8 @@ examples of things that can help:

instead of creating a short-lived temporary object. -

A somewhat more radical idea is to slice up multidimensional arrays into parallel -single one-dimension arrays:

+

A somewhat more radical idea is to slice up multidimensional arrays into +parallel single one-dimension arrays: