Nested loop optimization !!

Source: Internet
Author: User

The first part of the description: 1 put the big cycle into the internal test, the small cycle put on the outside, can indeed improve efficiency

/**
* Comprehensively test the performance of nested multi-layer for loops.
*
* @ Author laozizhu.com)
*
*/
Public class testforloop {
Public static void main (string [] ARGs ){
Int small = 1;
Int middle = 1000;
Int large = 1000000;
// The large loop is outside, the small loop is inside, and the variable is generated each time.
Long T = system. currenttimemillis ();
For (INT I = 1; I <= large; I ++ ){
For (Int J = 1; j <= middle; j ++ ){
For (int K = 1; k <= small; k ++ ){
}
}
}
System. Out. println (system. currenttimemillis ()-t );
// The large loop is outside, the small loop is inside, and the variables are generated in a unified manner.
T = system. currenttimemillis ();
Int I, J, K;
For (I = 1; I <= large; I ++ ){
For (j = 1; j <= middle; j ++ ){
For (k = 1; k <= small; k ++ ){
}
}
}
System. Out. println (system. currenttimemillis ()-t );
// The small loop is outside, the large loop is inside, and the variable is generated each time.
T = system. currenttimemillis ();
For (int kk = 1; KK <= small; KK ++ ){
For (int jj = 1; JJ <= middle; JJ ++ ){
For (int ii = 1; II <= large; II ++ ){
}
}
}
System. Out. println (system. currenttimemillis ()-t );
// Small loops are outside, large loops are inside, and variables are generated in a unified manner
T = system. currenttimemillis ();
Int II, JJ, KK;
For (KK = 1; KK <= small; KK ++ ){
For (JJ = 1; JJ <= middle; JJ ++ ){
For (II = 1; II <= large; II ++ ){
}
}
}
System. Out. println (system. currenttimemillis ()-t );
}
}


Test Run result

5625
3125
609
594


Note: The results of different machines are certainly different, and may be the following results:

5641
3140
594
625


Test conclusion:

1. Put a large cycle in the internal test, and put a small cycle on the external side, which can indeed improve efficiency.

In principle, the declaration of 2 variables can improve efficiency (from the first point of view, the effect is very different), but when the number of cycles is small, it is not obvious, sometimes it will affect efficiency.

Analysis:
Internal, external, and small
For (int K = 0; k <10; k ++ ){
For (Int J = 0; j <100; j ++ ){
For (INT I = 0; I <1000; I ++ ){
Function (I, j, k );
}
}
}
K <10; k ++; execute 10 times
J <100; j ++ executes 10*100 times
I <1000; I ++ executes 10*100*1000 times
Function (I, j, k); execute 10*100*1000 times
Total number of statements executed = (10 + 10*100 + 10*100*1000) * 2 + 10*100*1000 = 3002020
Inner, small, and large
For (int K = 0; k <1000; k ++ ){
For (Int J = 0; j <100; j ++ ){
For (INT I = 0; I <10; I ++ ){
Function (I, j, k );
}
}
}
K <1000; k ++; 1000 executions
J <100; j ++ executes 1000*100 times
I <10; I ++ executes 10*100*1000 times
Function (I, j, k); execute 10*100*1000 times
Total number of statements executed = (1000 + 1000*100 + 10*100*1000) * 2 + 10*100*1000 = 3202000

Therefore, the execution efficiency should be greater than internal, external, and write.
Inner, small, and large-inner, large, and small = 3202000 statements-3002020 statements = 199980 statements

 

Part 2: change from ++ --

For (INT I = 1000; I> 0; I --){
For (Int J = 100; j> 0; j --){
For (int K = 10; k> 0; k --){
Function (I, j, k );
}
}
}

 

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.