Rookie wants to be a architect. (ii)--java performance optimization for loop

Source: Internet
Author: User
Tags try catch

Complete the same function, with different code to achieve, there may be a large difference in performance, so for some performance-sensitive modules, it is necessary to optimize the code. Today, let's talk about Java code optimization, and today we're going to focus on optimizations for the for (while and so on) loops.


As one of the three major structures of the cycle, we are often used when writing code. The loop structure allows us to manipulate arrays, collections, and other regular things to become more convenient, but if we use them in actual development, it can have a great impact on the performance of the program. So we still need to master some tricks to optimize our code.


Nested loops

        Strattime = system.nanotime ();          for (int i = 0; i < 10000000; i++) {            for (int j = 0; J < Ten; J + + ) {                            }
    }        = system.nanotime ();        System.out.println ("Outside the inner Small time:" + (Endtime-strattime));        

should read:

        Strattime = system.nanotime ();          for (int i = 0; I <10; i++) {            for (int j = 0; J < 10000000; J + + ) {                            }
    }        = system.nanotime ();        System.out.println ("Outside small inside large time:" + (Endtime-strattime));


The two are time-consuming comparisons:

Small outside the large time: 200192114 outside the small inside big time: 97995997

From the above comparison, the performance of the optimization is increased by one times, nested loops should follow the " outside small inside big " principle, this is like you copy a lot of small files and the difference between copying several large files.


Extracting an expression unrelated to a loop

        Fstrattime = system.nanotime ();          for (int i = 0; i < 10000000; i++) {            i=i*a*b;        }         = system.nanotime ();        System.out.println ("Not extracted Time:" + (Endtime-strattime));


should read:

        Strattime = system.nanotime ();         = A +b        ;  for (int i = 0; i < 10000000; i++) {            i=i*c;        }         = system.nanotime ();        System.out.println ("Extracted time:" + (Endtime-strattime));

The two are time-consuming comparisons:

Not extracted time: 45973050 extracted time: 1955

A+b in the code is not related to our loop, so it should be placed outside, to avoid repeated calculations, it can be seen that the performance of the optimization after several orders of magnitude, these are not to be ignored.


Method call to eliminate loop termination judgment

        Strattime = system.nanotime ();          for (int i = 0; i < list.size (); i++) {                    }        = system.nanotime ();        System.out.println ("Not Optimized list time:" + (Endtime-strattime));

should read:

        Strattime = system.nanotime ();         int size = list.size ();          for (int i = 0; i < size; i++) {                    }        = system.nanotime ();        System.out.println ("Optimize list time:" + (Endtime-strattime));


The two are time-consuming comparisons:

Not optimized list time: 27375 optimization list time: 2444

List.size () each cycle will be executed once, which will undoubtedly affect the performance of the program, so it should be placed outside the loop, with a variable to replace, optimization before and after the contrast is also very obvious.


Exception capture

        Strattime = system.nanotime ();          for  (int i = 0; i < 10000000; i++) {            try  {            catch  (Exception e) {            }        }        = system.nanotime ();        System.out.println ("The internal catch exception takes time:" + (Endtime-strattime));


should read:

        Strattime = system.nanotime ();         Try  {        catch  (Exception e) {            for (int i = 0; i < 10000000; i++) {            }        }        = system.nanotime ();        System.out.println ("Catch exceptions on the outside time:" + (Endtime-strattime));

The two are time-consuming comparisons:

Capturing exceptions internally takes time: 12150142 external catch exceptions time consuming: 1955


As you all know, catching exceptions is very resource-intensive, so don't talk about try catch in the loop, and after optimization there are several orders of magnitude of ascension.


There are many performance optimizations, and code optimization is just a small part of it, and we should develop good coding habits in our daily development. Next, we will discuss with you more about performance optimization, I hope you can actively exchange guidance.


Rookie wants to be a architect. (ii)--java performance optimization for loop

Related Article

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.