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

5 core skills for writing titles for new media operations!

I despise clickbait titles, but sometimes it’s mo...

Review: 27 fans pushed the second time, how did I achieve 1,600 readers

I am a novice when it comes to operating a public...

How does Toutiao build the user life cycle?

I have met some friends who can talk eloquently a...

How to optimize headline information flow ads?

Whether it is operated by Party A or Party B, in ...

Google quietly launches public beta version of Chrome for iOS with 3D Touch

[[156222]] If users want to use more advanced Goo...

Why do those marketers make low-quality ads but make more money than you?

Let me introduce two people to you. Do you think ...

A guide to avoiding pitfalls when promoting KOLs

Inspired by the recent criticism of traffic fraud...

Without bicycles, we can't even take a plane!

June 3rd of each year is World Bicycle Day. The b...

10 insights into brand marketing!

The importance of insight is actually a helpless ...

World's first! Gene-edited pig heart transplanted into human body

The University of Maryland School of Medicine iss...