1. First we look at a piece of code:
for (int i=0;i<1000;i++) { for (int j=0;j<100;j++) { for (int k=0;k<10;k++) { testfunction (i,j,k);}} }
From the code shown, no matter how optimized, the number of times the testfunction executes is the same, and there is no possibility of optimization in that part. Then, the optimization of the code can only be analyzed from time-consuming aspects such as the instantiation, initialization, comparison, and self-increment of the cyclic variables I, j, K.
First, we analyze the time-consuming situation of the original topic code loop variables in instantiation, initialization, comparison, self-increment and so on:
(Note: Due to a single time time depending on the different machine configuration, the above table related time to use the number of processing to explain)
The performance optimization of this code is to minimize the number of instantiation, initialization, comparison, and increment of the loop variable I, j, and K, as well as to introduce other possible time-consuming operations.
2. resolution process
(1) Optimization scheme one
for (int i = 0; i < ten; i++) for (int j = 0; J < + j ++ ) for (int k = 0; k <; k++)
The program is mainly to put the least number of cycles outside, the most cycle of the put inside, so that the maximum (note: 3 different times of the cyclic variable total of 6 permutations, this combination is optimal) to reduce the number of the relevant cyclic variable instantiation, number of initialization, number of comparisons, the number of self-increment, The time-consuming scenario is as follows:
The original code: I need to compare 1000 times, J need to compare 1000*100 times, K need to compare 1000*100*10 times.
The total number of comparisons is 1000+1000*100+10*100*1000 times.
Now optimization scheme one: But K need to compare 10 times, J need to compare 10*100 times, I need to compare 10*100*1000 times.
The total number of comparisons is 10+10*100+10*100*1000 times.
So the following code is less than the previous code compared 1000+1000*100-(10+10*100) times!
(2) Optimization Program II
int I, J, K; for (i = 0; i < i++) for (j = 0; J < K; J + +) for (k = 0; k < k++) TestFunction (k, J, i);
On the basis of scenario one, this scheme puts the instantiation of the cyclic variables out of the loop, which can further reduce the number of instances of the related cyclic variables, and the scheme time-consuming situation is as follows:
Variable |
Instantiation (number of times) |
Initialization (number of times) |
Comparison (number of times) |
Self-increment (number of times) |
I |
1 |
1 |
10 |
10 |
J |
1 |
10 |
10 * 100 |
10 * 100 |
K |
1 |
10 * 100 |
10 * 100 * 1000 |
10 * 100 * 1000
|
3. Summary
According to the analysis of three tables in the process of case analysis and solution, the performance of optimization scheme and optimization Scheme two is better than that of the original code, in which the performance of optimization scheme two is the best . In a nested for loop, a loop with many loops on the inside , a loop with a small number of loops placed on the outside side , the performance will improve , and the performance of the loop variable is improved by reducing its instantiation . From the test data, it can be seen that for two optimization schemes, the operation effect is not very different if the number of cycles is small, but the effect is more obvious in the case of more cycles.
Android (Java) Learning Note 195: Sanchong for loop optimization (Java interview questions)