In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to optimize the performance of Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The Java code in Android will need to be compiled and optimized before execution. Different ways of writing the code will affect the optimization efficiency of the Java compiler. For example, different writing methods of the for loop will have different efficiencies for the compiler to optimize this piece of code, and when the program contains a large amount of such optimizable code, there will be operational performance problems. If you want to know how to optimize the operational performance of your code, you need to know the difference in the execution of the code at the hardware level.
2) Slow Function Performance
If you write a piece of code, its execution efficiency is much worse than expected. We need to know what factors may affect the execution efficiency of this code. For example, the execution time of comparing the size of two float values is about 4 times that of int values. This is due to the operational architecture of CPU, as shown in the following figure:
Although the modern CPU architecture has been greatly improved, there may not be such a big difference as shown above, but this example shows that differences in code writing can have a great impact on computing performance.
Generally speaking, there are two kinds of inefficient situations: the first is the relatively long execution time, we can easily find these methods and do some optimization. The second method is that the execution time is short, but the execution frequency is very high, because the number of execution is large, the cumulative effect will have a great impact on the performance.
To fix these detail efficiency issues, you need to use the tools provided by Android SDK, make careful measurements, and then fine-tune the fix.
3) Traceview Walkthrough
Open the Android Device Monitor through Android Studio, switch to the DDMS window, click the process you want to track above the left bar, and then click the Start Method Tracing button above, as shown below:
After starting tracking, manipulate app to do some of the events you want to track, such as swiping listview, clicking on some views to enter another page, and so on. When you're done, go back to Android Device Monitor and click the Method Tracing button again to stop tracking. At this point, the tool generates a detailed view of the TraceView for the operation you just did.
I will not expand here on how to view the detailed data in TraceView. There are many articles about it.
4) Batching and Caching
In order to improve computing performance, here are two very important technologies, Batching and Caching.
Batching is a batch preprocessing of data before actually performing an operation. For example, you need to have a method to find out whether a value exists in a pile of data. Assuming a premise, we will first sort the data, and then use the binary search method to determine whether the value exists. Let's first look at the * * cases. There are repeated sorting operations in the following figure.
In the above way, it should be acceptable if the order of magnitude of the data is not large, but if the data set is very large, there will be serious efficiency problems. So let's take a look at the improved writing, so that the sorting operation is packaged and bound only once:
The above is an example of Batching: pick out repetitive operations and package them only once.
The idea of Caching is easy to understand and is embodied in many ways. Here is an example of a for loop:
The above two basic skills are very practical, and positive 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 have the code execute on which thread. We all know that the Main Thread of Android is also UI Thread, which needs to undertake the feedback of users' touch events, the rendering of interface views and other operations. This means that we cannot do any non-lightweight operations in Main Thread. Operations such as IUnip O will take a lot of time, which may lead to frame loss in interface rendering and even lead to ANR. The solution to prevent these problems is to move code that may have performance problems to non-UI threads.
6) Container Performance
Another operational performance issue that we need to pay attention to is the reasonable choice of the basic algorithm, such as the performance difference between bubble sorting and fast sorting:
To avoid repeating wheels, Java provides many off-the-shelf containers, such as Vector,ArrayList,LinkedList,HashMap, etc., and there is a new SparseArray in Android. We need to understand the performance differences and applicable scenarios of these basic containers. Only in this way can the appropriate container be selected to achieve the performance of *.
This is the end of "how to optimize the performance of Android". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.