1, length, length (), size of the optimization
Example:
int array_one[] = {1,2,3,4,5,6,7,8,9,10}; int array_two[] = {1,2,3,4,5,6,7,8,9,10,11..100}; for (int i=0;i<array_one.length;i++) { for (int k=0;k<array_two.length;k + +) { dosth (); }}
In the above code, the system needs to calculate the array length for each for loop, and the overhead is naturally increased, so we can:
int array_one[] = {1,2,3,4,5,6,7,8,9,10}; int array_two[] = {1,2,3,4,5,6,7,8,9,10,11..100}; int length_array_one = array_one.length; int length_array_two= array_two.length; for (int i=0;i<length_array_one;i++) { for (int k=0;k<length_ array_two;k++) { dosth (); }}
Similarly, when calculating jsonarray or other arrays, you can optimize them in the same way.
2, for, while optimization
int array[] = {1,2,3,4..100}; int length_array = array.length; for (int i=0;i<length_array;i++) // Let's say we look for elements that are equal to 20 in array { if(i = =) { dosth (); }}
If you have completed the specified business in the loop, and there is no other business at this time, continue with the for loop so that the time cost increases and we can optimize it:
int array[] = {1,2,3,4..100}; int length_array = array.length; for (int i=0;i<length_array;i++) // Let's say we look for elements that are equal to 20 in array { if(i = =) { dosth (); Break ; }}
Jump out of the loop at the right time. Similarly, if it is a while loop, adjust the loop condition of the while to false, and then exit to
3. Optimization of variable life cycle
int array[] = {1,2,3,4..100}; int length_array = array.length; for (int i=0;i<length_array;i++) { new string[1000]; Dosth (str); // STR is used only as a parameter and does not modify it }
Look at the above code, each loop has a new array, the system's space use cost increases we can do this:
int array[] = {1,2,3,4..100}; int Length_array =new string[1000]; for (int i=0;i<length_array;i++) { dosth (str); // STR is used only as a parameter and does not modify it }
This kind of adjustment, only need to create a new once, effectively reduced the space cost
Android Code optimization technology record