Android Code optimization technology record

Source: Internet
Author: User

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

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.