Android Performance Optimization: Computing

Android Performance Optimization: Computing

Google recently released an online course on Android performance optimization on Udacity, which introduces how to optimize performance from the aspects of rendering, computing, memory, and power. These courses are a refinement and supplement of the Android performance optimization classic course previously released by Google on Youtube.

The following are the study notes for the operation chapter. Some of the content overlaps with the previous performance optimization examples. Everyone is welcome to study and communicate together!
1)Intro to Compute and Memory Problems

Java code in Android needs to go through the process of compilation optimization and execution. Different ways of writing code will affect the optimization efficiency of the Java compiler. For example, different ways of writing for loops will have different efficiencies for the compiler to optimize this code. When the program contains a large amount of such optimizable code, computing performance will be a problem. If you want to know how to optimize the computing performance of the code, you need to know the execution differences of the code at the hardware level.
2) Slow Function Performance

If you write a piece of code and its execution efficiency is much worse than you expected, we need to know what factors may affect the execution efficiency of this code. For example, the execution time of comparing two float values ​​is about 4 times that of int values. This is caused by the CPU's computing architecture, as shown in the following figure:

Although modern CPU architectures have been greatly improved and the differences shown above may not be as great, this example shows that differences in code writing can have a significant impact on computing performance.

Generally speaking, there are two types of poor operating efficiency: the first type is methods with relatively long execution time, which we can easily find and optimize. The second type is methods with short execution time but high execution frequency, which will have a great impact on performance due to the cumulative effect of many executions.

Fixing these detailed efficiency issues requires using the tools provided by the Android SDK, taking careful measurements, and then fine-tuning the fixes.
3) Traceview Walkthrough

Open the Android Device Monitor in Android Studio, switch to the DDMS window, click the process you want to track on the left column, and then click the Start Method Tracing button above, as shown below:

After starting the trace, manipulate the app and do some events you want to trace, such as sliding a listview, clicking on some views to enter another page, etc. After the operation, return to Android Device Monitor and click the Method Tracing button again to stop the trace. At this time, the tool will generate a detailed view of the TraceView for the previous operation.

As for how to view detailed data in TraceView, I will not go into details here, as there are many articles introducing it.
4) Batching and Caching

In order to improve computing performance, two very important technologies are introduced here: Batching and Caching.

Batching is the process of batch preprocessing data before actually performing an operation. For example, you need a method that can find out whether a value exists in a bunch of data. Assume that we sort the data first and then use binary search to determine whether the value exists. Let's look at the first case first. In the figure below, there are multiple repeated sorting operations.

In the above writing method, if the amount of data is not large, it should be acceptable, but if the data set is very large, there will be serious efficiency problems. Then let's look at the improved writing method, which packages the sorting operation and binds it to execute only once:

The above is an example of Batching: take out repeated operations, package them and execute them only once.

The concept of caching is easy to understand and is reflected in many aspects. Here is an example of a for loop:

The above two basic techniques are very practical, and their active and appropriate use can significantly improve computing performance.
5) Blocking the UI Thread

Improving the computational efficiency of the code is one aspect of improving performance, and it is also important to decide which thread the code is executed on. We all know that Android's Main Thread is also the UI Thread, which is responsible for responding to user touch events, rendering interface views, and other operations. This means that we cannot do any non-lightweight operations in the Main Thread, as I/O operations will take a lot of time, which is likely to cause frame drops in interface rendering and may even cause ANR. The solution to prevent these problems is to move the code that may have performance issues to a non-UI thread for operation.
6)Container Performance

Another performance issue we need to pay attention to is the reasonable choice of basic algorithms, such as the performance difference between bubble sort and quick sort:

To avoid reinventing the wheel, Java provides many ready-made containers, such as Vector, ArrayList, LinkedList, HashMap, etc. Android also has newly added SparseArray, etc. We need to understand the performance differences and applicable scenarios of these basic containers. Only in this way can we choose the right container to achieve the best performance.

<<:  Android performance optimization: rendering

>>:  Android performance optimization memory

Recommend

Tik Tok traffic rules, playback volume from 1 to 10 million!

The reason why Tik Tok is so popular is definitel...

Accenture: 2017 Medical Internet of Things Report

199IT original compilation Accenture released the...

50% of the creative copywriting for information flow ads is incomprehensible!

About 50% of the creative copywriting in informat...

More than 1/3 of the elderly suffer from insomnia. What can we do?

Myth: "Old people often wake up at night and...

International Polar Bear Day: As sea ice melts, where will they go?

February 27th of every year It's Internationa...

How will Lenovo operate after Motorola returns to China?

Xiaomi held the title of "world's third&...

A new side business with huge profits, can you make 200,000 a month?

A new side business with huge profits, can you ma...

Pinduoduo teaches three “secrets” to new media operations!

" Pinduoduo , Pinduoduo, the more you work, ...

User operation: a new way to increase user base through fission!

What methods were used to attract new users, resu...