Preface This article mainly introduces some small details of optimization techniques. When these small techniques are used in combination, they are still useful for improving the performance of the entire Android App, but they cannot significantly improve the performance. Choosing the right algorithm and data structure should be your primary consideration, which will not be covered in this article. You should use the tips in this article as a habit for writing code in your daily life, which can improve the efficiency of your code.
Code performance optimization suggestions Generally speaking, efficient code needs to meet the following two rules:
The execution effect of the code will be affected by many factors such as device CPU, device memory, system version, etc. In order to ensure that the code can run well on different devices, it is necessary to optimize the efficiency of the code. Avoid creating unnecessary objects Although GC can recycle unused objects, allocating memory for these objects and recycling them also consumes resources. Therefore, please try to avoid creating unnecessary objects. Here are some examples to illustrate this problem:
Choose Static instead of Virtual If you don't need to access the value field of an object, make sure the method is static, so that the method call will be 15%-20% faster. This is a good habit because you can know from the method declaration that the call cannot change the state of the object. Constants are declared as Static Final First look at the following statement
The compiler will use the initialization method to initialize the above value when the class is used. When accessing it later, it will need to look it up first before returning the data. We can use static final to improve performance:
At this time, you no longer need the above method to perform redundant lookups. Therefore, please declare constants as static final types as much as possible. Avoid Internal Getters/Setters In native languages like C++, it is common to use getters (i = getCount()) instead of accessing variables directly (i = mCount). This is a good habit to write in C++, and is often adopted by other object-oriented languages such as C# and Java, because the compiler usually inlines access, and you can add code in getters/setters at any time if you need to restrict or debug variables. However, on Android, this is a bad practice. Virtual method calls are more expensive than direct access to variables. So the reasonable approach is: use getters/setters in object-oriented design, but you should access variables directly inside the class. Without JIT (Just In Time Compiler), direct access to variables is 3 times faster than calling getters. With JIT, direct access to variables is 7 times faster than accessing through getters. Note that if you use ProGuard, you can get the same effect because ProGuard can inline accessors for you. Using the enhanced For loop Compare the following three looping methods:
zero() is the slowest because the JIT cannot optimize it. one() is slightly faster. two() is the fastest without JIT, but after JIT, it is almost as fast as method one(). It uses the enhanced loop method for-each. Use package-level access instead of private access for inner classes Refer to the following code
We have defined a private inner class (Foo$Inner) that directly accesses private methods and private member objects in the outer class. This is legal and the code will print "Value is 27" as expected. The problem is that VM thinks it is illegal to directly access private members of Foo class in (Foo.Inner) because Foo and (Foo.Inner) are different classes. Even though Java language allows inner classes to access private members of outer classes. To remove this difference, the compiler generates some mock functions:
Whenever the inner class needs to access the mValue member in the outer class or needs to call the doStuff() function, it calls these static methods. This means that the code above can be reduced to accessing member variables through accessor functions. Earlier we said that accessing through accessors is slower than accessing fields directly. So, this is an example of a specific language usage causing performance degradation. If you are using code like this in a performance hotspot, you can declare the fields and methods that inner classes need to access as package-level access instead of private access. Unfortunately, this means that other classes in the same package can also access these fields directly, so you can't do this in the public API. Avoid using float types In the Android system, the data access speed of the float type is half that of the int type. Try to give priority to the int type. Using library functions Try to use some encapsulated library functions such as System.arraycopy(), which are more than 9 times more efficient than manually writing copy implementations. Tip: Also see Josh Bloch's Effective Java, item 47. Use native functions with caution When you need to migrate existing native code to Android, please use JNI with caution. If you want to use JNI, please learn JNI Tips Misconceptions about performance Before JIT, using a specific data type is indeed more efficient than using an abstract data type. (For example, using HashMap is more efficient than Map.) There is a misconception that the efficiency is twice as high, but it is actually only about 6%. Moreover, after JIT, there is not much difference between them. About Measurement The data in the above document is the actual running effect of Android. We can use Traceview to measure, but the measured data is not JIT optimized, so the actual effect should be slightly better than the measured data. |
<<: The last unknown blue ocean of Android emulator mobile games
>>: Anti-aliasing processing method for image deformation
In recent years, you must have noticed these chan...
In the tablet computer field, whether it is Apple...
After switching to Fenix, Mozilla has added most ...
Account optimization refers to the process of con...
From the other side of the ocean, known for its c...
Why is my ad not getting any exposure no matter h...
When you vacuum, sweep the floor, and wipe the fu...
Mini programs are becoming popular again. Recentl...
The first Douyin follower growth list in the seco...
Walking may only require you to prepare a pair of...
A good friend of mine is a very famous business c...
I have been in the Internet circle for 6 years an...
Today let’s talk about a not very interesting que...
The o2o company I work for is a one-stop communit...