Java language nesting for loop performance optimization Case __java

Source: Internet
Author: User
1 Case Description
One day, on the Javaeye to see an interview question, the title is this: please optimize the following code
Java code for (int i = 0; i < 1000. i++) for (int j = 0; J < + +) for (int k = 0; k < k++) ) TestFunction (I, J, K);
(Note: In order to be consistent with the contents of the following, the original topic has been partially modified)

2 Case Study
From the code given, no matter how optimized, the number of testfunction execution is the same, and there is no possibility of optimization in this part. Then, the optimization of code can only be analyzed from the time consuming in the aspects of instantiation, initialization, comparison, and increment of cyclic variables I, j and K.
First, we first analyze the time consuming situation of the original code loop variable in instantiation, initialization, comparison, and increment:
Variable Instantiated (number of times) Initialization (number of times) Comparison (number) Self Increase (number)
I 1 1 1000 1000
J 1000 1000 1000 * 100 1000 * 100
K 1000 * 100 1000 * 100 1000 * 100 * 10 1000 * 100 * 10

(Note: Because a single time consuming depending on the different machine configuration, the previous table related to the time to use the number of processing to explain)
The performance optimization of the code is to minimize the number of instances, initializations, comparisons, and increases of the cyclic variables I, j, K, and not to introduce other possible operation time.

3 Settlement Process
From the case analysis, for the original code, we propose that there are two kinds of optimization scheme:
3.1 optimization Scheme one
The code is as follows:
Java code for (int i = 0, i < i++) for (int j = 0; J < + j) for (int k = 0; k < 1000; k++ ) TestFunction (k, J, I);
The program is mainly to put the least number of cycles outside, the maximum number of cycles in the inside, this can be the largest (note: 3 different times of the cyclic variable a total of 6 permutations and combinations, this kind of combination is optimal) to reduce the number of related cycle variables, initialization times, comparison times, the number of times, since the increase, The scenario is time-consuming as follows:
Variable Instantiated (number of times) Initialization (number of times) Comparison (number) Self Increase (number)
I 1 1 10 10
J 10 10 10 * 100 10 * 100
K 10 * 100 10 * 100 10 * 100 * 1000 10 * 100 * 1000


3.2 Optimization Scenario two
The code is as follows:
Java code int I, j, K; for (i = 0; i < i++) for (j = 0; J < + J) for (k = 0; k < 1000; k++) testf Unction (k, J, I);
On the basis of the scheme one, the instantiation of the cyclic variable is put outside the loop, which can further reduce the number of instances of the related cyclic variables, and the program time consuming is as follows:
Variable Instantiated (number of times) Initialization (number of times) Comparison (number) Self Increase (number)
I 1 1 10 10
J 1 10 10 * 100 10 * 100
K 1 10 * 100 10 * 100 * 1000 10 * 100 * 1000


4 Settlement Results
So, does the proposed optimization have a performance boost as we analyzed it? We write some test code to verify that the data can better illustrate our optimization results.
4.1 test Code
Java Code    public static void testfunction (int i, int j, int k)  {           system.out.print ("");    //   Note: This method does not affect overall optimization, here only simple output        }           public static void testa ()  {            long start = system.nanotime ();            for  (int i = 0; i < 1000; i++)                 for  (int j = 0; j <  100; j++)                     for  (int k = 0; k < 10; k++)         &Nbsp;               testfunction (I,  j, k);           system.out.println ("testA  Time>> " +  (System.nanotime ()  - start));       }           public static void testb ()  {            long start = system.nanotime ();            for  (int i = 0; i < 10; i + +)   

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.