First, advanced optimization
Mainly from the primary optimization of the way, this article will be from the point of view of program performance, analysis of the shortcomings of various common scenarios, and give the object pool technology, basic data type substitution method, shielding function calculation three can save resource overhead and Processor Time optimization strategy.
The most commonly used optimization options are:
· Optimize loops to improve the performance of loop bodies by re-organizing repetitive sub-expressions
· Reduce the number of objects used to improve operational performance
· Reduce network transmission data to shorten wait times, etc.
This article learns three other strategies for performance optimization:
1. Using object pooling technology to improve the utilization of objects
Creating and releasing objects in Java consumes considerable resources and uses object pooling techniques to improve the utilization of objects
For example: in-game enemy aircraft processing method: One is the game when loading the level, for each enemy aircraft to create an object, such a scenario in which the resource cost of creating objects is huge, which seriously affects the performance of mobile games. Method Two is the game process, the dynamic creation of enemy objects, destroyed after the object is set to null and by System.GC () recycling.
game performance loss is primarily the creation and release of objects, not the creation of objects and the implementation of logical functions, so as far as possible to avoid object creation and release.
Solution: Create a Quantitative object based on demand, request a free object from the object pool when needed to create the object, and release the object back to the pool when releasing the object to effectively avoid the performance penalty caused by creating and releasing objects
in the above example, the demand is not more than 5 enemy aircraft, so you can use the following code:
ENEMY[5] Enemy = new Enemy[5];for (int i = 0; i<5;i++) {Enemy[i] = new Enemy ();}
in class enemy the Add flag attribute used and the reset method with parameters enable the object to be reset to its original state, initialize the object pool when loading the game level, and obtain an unused object pool when it is necessary to create objects The object used and initialized with the reset method requires that the object be disposed only when the flag bit is modified for the next use.
2. Local use of basic data types in place of objects, saving resource overhead
3. Save processor Time by using simple numerical calculations instead of complex function calculations
Second, Android efficient development
Two basic principles: don't do what you don't need to do, and save as much memory as possible.
1. Avoid creating objects as much as possible. The following optimization cases:
· When extracting a string from the original input data, try to return a substring from the original string instead of creating a copy. You will create a new string object, but it shares the data space with your original data.
· If you have a method that returns a string, you should know that the result of the return is stringbuffer, change the definition and execution of your function, and let the function return directly instead of creating a temporary object.
· An array of type int is better than an array of Integer types, but it is also possible to generalize the principle that an array of two int types is much more efficient than an array of (int, int) objects. The same is true for other underlying data types.
• Two parallel foo[] and bar[] are much more efficient than an array of (Foo,bar) objects
in general, we should avoid creating short-term temporary objects as much as possible. Less object creation means less garbage collection, which improves the quality of your program's user experience.
2. Use your own method
when dealing with strings, do not hesitate to use as many methods as possible, such as: String.IndexOf (), String.LastIndexOf (), to the object itself. Because these methods are implemented in C + +, 10~100 times faster than Java loops
3. Using virtual better than using interfaces
Assuming you have a HashMap object, you can declare that it is a HashMap or just a Map, as follows:
Map myMap1 = new HashMap (); HashMap myMap2 = new HashMap ();
It is generally wise to use map because it allows you to change the map interface to perform anything above it. Calling through an interface reference can take more than twice times longer than a call to a virtual function through a specific reference.
4. Using static better than using virtual
If you don't have to go to the outside of the object, make your method static. It will be called faster because it does not require a virtual function-oriented table. Calling this method does not change the state of the object.
5. Avoid using the internal Get,set method as much as possible
calls to virtual methods incur many costs, more than instance property queries. We should use the Get and Set functions when calling externally, but we should call them directly when we call them internally.
6. Buffering Property Calls
for (int i = 0; i < This.mcount; i++) {Dumpitem (this.mitems[i]);}
It should be written like this:
int count = this.mcount;for (int i = 0; i < count; i++) {Dumpitem (this.mitems[i]);}
A similar principle is: Never call a method of a class for the second time in a for statement.
7. Affirm Final Constants
static string strval = "n"; static final String strval = "100";
the non-final compiler invokes a class initialization method <clinit>, which extracts a reference from the class file string constant table for Strval. Final is not called because these constants are directly written to the class file static property initialization, which is handled directly by the virtual machine.
declaring a class or method as "final" does not bring any execution benefits, it can be optimized for a certain degree of processing. If the compiler knows that a get method cannot be overridden by a class, then it should set the function to Inline
8. Use enhanced for loop statements with caution
In other collectors, the enhanced for loop is equivalent to the use of iterator.
9. Avoid enumeration types
10. Using the package space through inline classes
11. Avoiding the use of floating-point types
embedded processors typically do not support floating-point processing, so all float and double operations are software-based, and some basic floating-point operations take a millisecond-level time.
Third, Android UI optimization
1. Relativelayout and LinearLayout use less resources to achieve the same purpose in resource utilization.
Relativelayout It is important to note that the interior is a framework that is determined by the relationships between multiple view, so when one of the view is completely hidden because some need to call GONE, it will affect its associated 's Views. The solution is to use the Alignwithparentifmissing property to solve the type problem.
2. <viewstub/> This label can make the UI in special cases, the visual effect is similar to setting the view's invisibility, but the bigger point is that the view wrapped by this tag does not occupy any memory space by default. Viewstub import View elements from outside through include. Usage is to specify the content that is contained by Android:layout. By default, Viewstub contains labels that belong to visibility = GONE. Viewstub uses the method inflate () to summon the system to load its internal View.
<viewstubandroid:inflatedid= "@+id/subtree" android:layout= "@layout/mysubtree" android:layout_width= "120DP" android:layout_height= "40DP"/>
<include/>: This tab allows you to load external XML directly into the current structure and is a common label for reusing UI resources.
The <requestfocus/> Label is used to specify the focus view within the screen. The usage is to label the inside of the View tag
<edittextandroid:layout= "@layout/mysubtree" android:layout_width= "120DP" android:layout_height= "40DP" >< Requestfocus/></edittext>
<merge/> Tags: plays an important role in optimizing the UI structure. The goal is to optimize the structure of the entire Android Layout by cutting redundant or extra layers.
Four, picture optimization
1. Image compression
The image reduction operation is to delete some pixels, and the small graph enlarges to artificially add some pixels. Picture enlargement is not always multiplied. When porting to a different phone, you can use the menu Canvas interface Getwith () and getheight () to get the current phone screen width and height, if the current picture is not enough full screen display to enlarge the picture, the opposite is reduced. You can also customize the screen to display the size of the picture, such as the display of multiple pictures on the screen thumbnail, so that the picture is more colorful.
2. Reduce picture size
· Integrate multiple images onto a single image. Because you omit multiple pictures of the file header, file end data block, etc., and merge the color palette
· Reduce the number of colors in a picture
Welcome reprint, Reprint Annotated source, thank you
Mr Fu: Reading from the secrets of Android app development