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

iOS: Let's talk about Designated Initializer

1. iOS object creation and initialization Object ...

Great news! A salt-tolerant rapeseed has emerged? | Expo Daily

my country has made a breakthrough in developing ...

BOE launches 110-inch "4K giant screen" to display 1:1 real-life images

[September 9 news] Yesterday, a message came from ...

What are server logs? How to read the server log?

Although many webmasters now understand search ra...

Why is short video promotion and marketing so popular? How to play?

Driven by the trends of video mobility, informati...

How to carry out advertising in the medical beauty industry? Case Analysis

Today, Qingguajun will share with you the analysi...

Douyin STOM full-link advertising solution

Today, I will also share with you my experience o...

Should you turn on iOS privacy tracking? You will understand after reading this

The official version of iOS14.5 has been released...

Soul Product Analysis Report

Lonely souls will attract each other. Different f...

Google launches black technology again: can help goddesses remove mosaics

There is nothing more annoying than having your p...

How to identify Baidu spider? How to attract Baidu spider?

When many SEO practitioners first come into conta...