Cainiao to be an architect (II) -- java Performance Optimization-for loop and java Performance Optimization

Source: Internet
Author: User
Tags try catch

Cainiao to be an architect (II) -- java Performance Optimization-for loop and java Performance Optimization

When the same function is completed and implemented using different codes, the performance may be significantly different. Therefore, for some performance-sensitive modules, it is necessary to optimize the code. Today I will talk about java code optimization. Today I will talk about optimization for the for (while and so on) loop.


As a loop of one of the three structures, we often use it when writing code. The loop structure makes it easier for us to operate arrays, collections, and other regular things. However, if we do not use them properly in actual development, it may have a great impact on the program performance. Therefore, we still need to master some skills to optimize our code.


Nested loop

StratTime = System. nanoTime (); for (int I = 0; I <10000000; I ++) {for (int j = 0; j <10; j ++) {}} endTime = System. nanoTime (); System. out. println ("Big, small, and time-consuming:" + (endTime-stratTime ));

Should be changed:

StratTime = System. nanoTime (); for (int I = 0; I <10; I ++) {for (int j = 0; j <10000000; j ++) {}} endTime = System. nanoTime (); System. out. println ("external time consumption:" + (endTime-stratTime ));

Time consumption comparison:

Large, small, and time-consuming: 200192114 small, large, and time-consuming: 97995997

From the above comparison, we can see that the performance is doubled after optimization, and the nested loop should follow the principle of "small inside, this is like the difference between copying many small files and copying several large files.


Extract loop-independent expressions

StratTime = System. nanoTime (); for (int I = 0; I <10000000; I ++) {I = I * a * B;} endTime = System. nanoTime (); System. out. println ("unextracted time:" + (endTime-stratTime ));

Should be changed:

StratTime = System. nanoTime (); c = a + B; for (int I = 0; I <10000000; I ++) {I = I * c;} endTime = System. nanoTime (); System. out. println ("extracted time consumption:" + (endTime-stratTime ));


Time consumption comparison:

Unextracted time: 45973050 extracted time: 1955

In the code, a + B has nothing to do with our loop. Therefore, we should put it out to avoid repeated computation. We can see that the performance has been improved by several orders of magnitude after optimization, which cannot be ignored.


Method call to eliminate loop termination judgment

StratTime = System. nanoTime (); for (int I = 0; I <list. size (); I ++) {} endTime = System. nanoTime (); System. out. println ("unoptimized list time consumption:" + (endTime-stratTime ));


Should be changed:

StratTime = System. nanoTime (); int size = list. size (); for (int I = 0; I <size; I ++) {} endTime = System. nanoTime (); System. out. println ("optimized list time consumption:" + (endTime-stratTime ));

Time consumption comparison:

Unoptimized list time consumed: 27375 optimized list time consumed: 2444


List. size () is executed once every cycle, which will undoubtedly affect the program performance. Therefore, it should be placed outside the loop and replaced by a variable. The comparison before and after optimization is also obvious.


Exception capture

StratTime = System. nanoTime (); for (int I = 0; I <10000000; I ++) {try {} catch (Exception e) {}} endTime = System. nanoTime (); System. out. println ("time taken to capture internal exceptions:" + (endTime-stratTime ));

Should be changed:

StratTime = System. nanoTime (); try {} catch (Exception e) {for (int I = 0; I <10000000; I ++) {}} endTime = System. nanoTime (); System. out. println ("time taken to capture exceptions externally:" + (endTime-stratTime ));


Time consumption comparison:

Internal exception capture time: 12150142 external exception capture time: 1955

As we all know, capturing exceptions is resource-consuming, so do not put try catch inside the loop. After optimization, there are also several orders of magnitude improvements.


There are many content for performance optimization, and code optimization is only a small part. We should develop good coding habits in daily development. We will discuss with you later

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.