Android app development secrets learning notes-Optimization

Source: Internet
Author: User

Java Syntax:
Correct use of Java. Lang. String and Java. util. Vector
1. Specify the final of the class as much as possible. The Java compiler will look for opportunities to inline (Inline) All final Methods
2. Try to reuse objects. Use stringbuffer instead of string connection.
3. Use local variables whenever possible. Parameters passed during method call and temporary variables created in the call are saved.
In the stack, other static instances and instance variables are in the heap.
4. Do not reinitialize the variable. This will happen when the derived class is used.
5. Try to use uppercase letters in Oracle to reduce the burden on the Oracle parser.
6. Use the database to connect. Disable the I/O Stream.
7. manually set the used object to null
8. When using the synchronization mechanism, try to use synchronous methods instead of code block synchronization.
9. reduce repeated variable calculation as much as possible
Example: For (INT I = 0; I <list. size; I ++ ){}
For (INT I = 0, int Len = List. Size (); I <Len; I ++ ){}
10. Try to use the lazy loading policy and create it only when necessary.
For example:
String STR = "AAA ";
If (I = 1 ){
List. Add (STR );
}
Replace:
If (I = 1 ){
String STR = "AAA ";
List. Add (STR );
}
11. Use exceptions with caution. Exceptions can only be used for error handling and should not be used to control the process.
12. Do not use try/catch in a loop
13. Using a suitable capacity value to initialize stringbuffer is always the best choice, because when it is full, it will increase (2 * old value + 2) Capacity
14. Properly use vector to delete elements from the end. In fact, you can also removeallelements ()
15. When copying a large amount of data, use the system. arraycopy () command
16. Code refactoring: Enhancing code readability
17. You do not need to create a new instance.
Using new will call all constructors, but if the object implements the cloneable interface, you can call its clone () method.
In this way, no class constructor is called. For example, factory Mode
Public static credit getnewcredit (){
Return new credit ();
}
After improvement, use the clone () method as follows:
Private Static credit basecredit = ne credit ();
Public static credit getnewcredit (){
Return (credit) basecredit. Clone ();
}
18. Changing multiplication and division to bitwise operations, for example, 5*4 can be changed to 5 <2
19. Do not declare the array as public static final;
20. The value of each hshmap traversal operation is better than that of the keyset operation.
21. Array (array) and arrylist usage, array ([]); is the most efficient, but its capacity is fixed and cannot be changed dynamically
The arraylist capacity can be changed without sacrificing efficiency.
22. Use hashmap and arraylist whenever possible. Unless necessary, hashtable and vector are not recommended. The latter uses a synchronization mechanism, resulting in performance overhead.
23. stringbuffer is thread-safe while stringbuilder is not

Advanced optimization:
1. Object pool:
Enemy [5] Enemy = new enemy [5];
For (INT I = 0; I <5; I ++ ){
Enemy [I] = new enemy ();
}
Add the sign attribute used and reset method with parameters to enemy so that the object can be reset to the initial state.
2. Try to replace the object with the basic type
For example, a bullet is abstracted into a bullet class, and the definition represents the bullet Object pool. In each gameloop
Reduce the ordinate value of the bullet object by a certain number, because the basic data type is more efficient.
Yes
Int [] [] Bullet = new int [2] [10]; // assume that the screen contains 10 bullets at the same time.
For (INT I = 0; I <10; I ++ ){
Bullet [0] [I] = 999; // corresponds to the xposition attribute of the bullet object
Bullet [1] [I] = 999; // corresponds to the yposition attribute of the bullet object
}
In each gameloop:
While (run ){
For (INT I = 0; I <10; I ++ ){
If (bullet [1] [I] = 99 ){
// Reset to the current position of the aircraft with bullets
Bullet [0] [I] = myplane. getxposition ();
Bullet [1] [I] = myplane. getyposition ();
} Else {
// Reduce the ordinate value of the currently used bullet
Bullet [I] [J]-= 10;
}
}
}
3. Replace complex function compute with simple numeric computing
For example, save the positive cosine value
Public static int iangle [] = new int [] [] {
// Define different precision based on game needs, which can be defined from 0 to 45 ° consecutively
// Because SiNx = cos (90-x), you do not need to repeatedly define the function values from 46 ° to 90 °.
// Degree, cosine, and sine
{0, 1000, 0 },
{5,996, 87 },
{10,985,174 },
{40,766,643 },
{45,707,707 },
}
 
Int [] [] enemybullet = new int [4] [20]; // assuming that 20 bullets are simultaneously displayed on the screen
Int size = enemybultet. lenth;
For (INT I = 0; I <size; I ++ ){
Enemybullet [0] [I] = 999; // X coordinate of the enemy plane bullet
Enemybullet [1] [I] = 999; // y coordinate of the enemy plane bullet
Enemybullet [2] [I] = 999; // The X coordinate displacement of the enemy plane bullet
Enemybullet [3] [I] = 999; // y coordinate displacement of the enemy plane bullet
}
Assign a specific value from the array iangle to the displacement according to the program's needs. enemybullet [2] [I], enemybullet [3] [I]
Int size = enemybullet. length;
For (INT I = 0; I <size; I ++ ){
Enemybullet [0] [I] + = enemybullet [2] [I];
Enemybullet [1] [I] + = enemybullet [3] [I];

}
 
 
Efficient Android Development
1. Avoid creating objects as much as possible
1) When extracting a string from the original input data, try to return
Instead of creating a copy, you will create a new String object, but share the data space with your original data.
2). If you have a method to return a string, you should know that the returned result is stringbuffer anyway and change your
Define and execute a function so that the function can return directly instead of creating a temporary object.
3). An int type is better than an integer array. The same two int type arrays are more efficient than an array of (INT, INT) objects.
The smaller the number of Creation means less garbage collection, which improves the user experience.

2. Use your own Method
When processing strings, do not hesitate to use as many objects as possible: String. indexof (), String. lastindexof ()
Because these methods are implemented using C/C ++, they are faster than in a Java loop.
3. The static method is better than the Virtual Interface.
4. Avoid using the internal get and set methods as much as possible.
Android programming has a lot of costs, so we use get and set in external calls, but directly call
5. Buffer property call
For (INT I = 0; I <shit. mcount; I ++ ){
Dumpitem (this. mitems [I]);
}
It should be written:
Int COUNT = This. mcount;
Item [] items = This. miems;
For (INT I = 0; I <count; I ++ ){
Dumpitems (items [I]);
}

6. Declare final Constants
Static int intval = 100;
Static string strval = "Hello, world ";
When the class is used for the first time, the compiler will call a class initialization method <clinit>, save 100 to the variable intval, and
Extract a reference for strval in the character constant table of the class file. When these values are referenced later, the attribute is called directly. We can use final to improve the code.
Static final int intval = 100;
Static final string strval = "Hello, world ";
In this way, the <clinit> method is not called and directly processed by the Virtual Machine. When the code accesses intval, 100 of the integer type is used.
Accessing strval will replace an attribute call with a relatively saved "String constant"
7. Exercise caution when using the enhanced for loop statement
8. Avoid Enumeration type
Easy to use, but the cost for size and speed is very high
9. Use the package space through inline classes
10. Avoid using the floating point type
 
 
 
 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.