System Performance Optimization (5) -- Java loop and string code optimization

Source: Internet
Author: User

Loop and string processing are worth noting during system performance optimization. In terms of mentality, we must not put our own eyes on ten or hundreds of cycles, nor treat the strings we want to process as ten or twenty characters. Every time a loop is encountered, it should be assumed that the loop is more than ten thousand times. Each time the string to be processed, you must tell yourself that the string may be very large in the future. Do not wait until the data volume reaches 100,000 or million before processing. In this case, the cost will be too high. This article describes how to optimize loops and strings in Java code.

About Loops

Nested for loops are placed on the inner side with a large number of times. As we all know, a loop variable needs to be defined for a for loop to traverse every object that requires a loop. If a loop with a large number of loops is placed on the outside, the overall variable will increase and the efficiency will naturally decrease. The following code tests

public class Test{public static void main (String [] args){Long time2Before=System.nanoTime();for (int i=0; i<10;i++ ){ for (int j=0; j<1000000;j++ ){ }}Long time2After=System.nanoTime();System.out.println("faster--->"+(time2After-time2Before));Long time1Before=System.nanoTime();for (int i=0; i<1000000;i++ ){ for (int j=0; j<10;j++ ){ }}Long time1After=System.nanoTime();System.out.println("slower--->"+(time1After-time1Before));}}

Do only loop-related tasks in a loop. Do not place unnecessary loops in the loop. For example, when traversing a set, there is no need to put the size of the Set in a loop, and it can be placed outside the set. There is no difference in performance, and there is a huge gap in performance.

import java.util.*;public class Test1{public static void main (String [] args){List<String> list=new ArrayList<String>();for(int i=0;i<1000000;i++){list.add("luck"+i);}Long time1Before=System.nanoTime();for(int i=0;i<list.size();i++){//System.out.println(list.get(i));}Long time1After=System.nanoTime();System.out.println("use .size-->"+(time1After-time1Before));Long time2Before=System.nanoTime();int n=list.size();for(int i=0;i<n;i++){//System.out.println(list.get(i));}Long time2After=System.nanoTime();System.out.println("do not use .size-->"+(time2After-time2Before));}}

About strings

Eliminate string connections. in the program, use stringbuffer or stringbuilder instead of string. A string is equivalent to an anonymous String object. If two strings are concatenated in a program, three string spaces are defined in the memory. The stringbuffer or stringbuilder will not do this, but will modify it in the existing stringbuffer or stringbuilder object. The test code is as follows:

public class Test3{public static void main (String [] args){long time1Before=System.nanoTime();String str="";for(int i=0;i<10000;i++){str+=i;}long time1After=System.nanoTime();System.out.println("use String --->  "+(time1After-time1Before));long time2Before=System.nanoTime();StringBuilder sbuilder=new StringBuilder();for(int i=0;i<10000;i++){sbuilder.append(i);}long time2After=System.nanoTime();System.out.println("use StringBuilder--->  "+(time2After-time2Before));long time3Before=System.nanoTime();StringBuffer stringBuffer=new StringBuffer();for(int i=0;i<10000;i++){stringBuffer.append(i);}long time3After=System.nanoTime();System.out.println("use StringBuffer--->  "+(time3After-time3Before));}}

It should be noted that it is very easy to choose between stringbuffer and stringbuilder. The former is thread-safe, and the latter is thread-unsafe. In other words, the latter is faster than the former. To sum up, if we only consider performance, the order from high to low is: stringbuilder --> stringbuffer --> string.

 

Loop and string are the easiest places in the program to improve code efficiency, because many people leave efficiency behind for the convenience of graph while writing the program. It doesn't matter when the data to be processed is not big, once the data to be processed by the Program increases, the performance bottleneck arises. So as mentioned at the beginning of the article, when writing a program, we need to consider the order of 100,000 million, and do not wait until the performance bottleneck arises to solve it, because the cost of code reconstruction or later optimization is much higher than the development cost in the early stage, I believe that the kids who have read the code without comments and lengthy Code have a deep understanding (sneer ~~).

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.