Complete the same function, with different code to achieve, the performance may be quite different, so for some performance-sensitive modules, it is necessary to optimize the code. Today we are going to talk about Java code optimization. Today we are going to talk about optimization for loops (while, etc.).

As one of the three structures, loops are often used when we write code. The loop structure makes it easier to manipulate arrays, collections, and other regular things, but if it is not properly used in the actual development, it may have a great impact on the performance of the program. So we still need to learn some tricks 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(" endtime-strattime ");Copy the code

Should be changed to:

stratTime = System.nanoTime(); for (int i = 0; i <10 ; i++) { for (int j = 0; j < 10000000; {}} endTime = system.nanotime (); System.out.println(" endtime-strattime ");Copy the code

Time comparison between the two:

It can be seen from the above comparison that the performance has been improved twice after optimization. The nested loop should follow the principle of “small on the outside and big on the inside”, which is just like the difference between copying many small files and copying a few large files.

Extract expressions that are independent of the loop

fstratTime = System.nanoTime(); for (int i = 0; i < 10000000; i++) { i=i*a*b; } endTime = System.nanoTime(); System.out.println(" unfetched time: "+(endtime-strattime));Copy the code

Should be changed to:

stratTime = System.nanoTime(); int size = list.size(); for (int i = 0; i < size; I++) {} endTime = system.nanotime (); System.out.println(" optimize list time: "+(endtime-strattime));Copy the code

Time comparison between the two:

List.size () is executed once in each loop, which will definitely affect the performance of the program, so it should be taken out of the loop and replaced by a variable. The comparison before and after optimization is also obvious.

Exception handling

stratTime = System.nanoTime(); for (int i = 0; i < 10000000; I++) {try {} catch (Exception e) {}} endTime = system.nanotime (); System.out.println(" Time to catch exceptions internally: "+(endtime-strattime));Copy the code

Should be changed to:

stratTime = System.nanoTime(); try { for (int i = 0; i < 10000000; } catch (Exception e) {} endTime = system.nanotime (); System.out.println(" time to catch exceptions externally: "+(endtime-strattime));Copy the code

Time comparison between the two:

Internal exception capture time: 12150142 External exception capture time: 1955

As you all know, catching exceptions is expensive, so instead of putting try catches inside the loop, optimization can be orders of magnitude better.

There are many content of performance optimization, code optimization is only a small part of it, we should develop good coding habits in daily development. We will talk more about performance optimization in the future, and we hope you can exchange your guidance.

The latest 2020 collection of some high frequency interview questions (are organized into a document), there are a lot of dry things, including mysql, Netty, Spring, thread, Spring cloud, JVM, source code, algorithm, etc., there are also detailed learning planning map, interview questions organized, etc., Please add 756584822 if you need to obtain these contents