Optimizing Java string connections and loops

Source: Internet
Author: User

Recently, when looking at the company project, found that the code written by predecessors rarely use the string through the "+" number splicing, through the source of the discovery package Java.lang under the package StringBuilder such a class of StringBuilder used up quite convenient, Especially when stitching SQL to see at a glance.

Online also said: Eliminate string connection, in the program is preferred to use StringBuffer or StringBuilder instead of string. A string is equivalent to an anonymous string object, and if you stitch two strings in a program, you define three string spaces in memory. StringBuffer or StringBuilder would not do so, but would be modified in the original StringBuffer or StringBuilder object. Unconsciously performance can also be optimized, decisive use it chant. It is important to note that between StringBuffer and StringBuilder, if one should be considered, the principle is simple, the former is thread-safe, the latter is thread insecure, in other words the latter is faster than the former. In summary, if performance considerations are high-to-low, they are: StringBuilder--StringBuffer--and String.

So how does it work? Because it is the code of the predecessor, I will not post it, I am more willing to write a few examples of their own

StringBuilder selectsql=new StringBuilder (); Selectsql.append ("Select * from T_user");

When it comes to performance optimization, there's a more typical loop.

Before the work before, really did not pay attention to the loop this piece, here also share with you:

1. Nested for loop in the number of times placed in the inside, the number of less on the outside side. Don't let us run a point ...

public class test1{    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 ();      &NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Slower--->" + (Time1after-time1before));     }}

Post three run results:

1.faster--->7299516slower--->96478422.faster--->16731410slower--->98995073.faster---> 17344766slower--->8592737

2. In the loop only do the loop-related things, some unnecessary loops do not put in the loop to do. For example, there is no need to put the collection size in a loop when iterating through the collection, which can be placed entirely outside the collection. The effect is not different, the performance gap is huge.

import java.util.*;p ublic 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));     }}

Post three run results:

1.use. size-->4907262do size-->41760792.use. Size-->7835279do not use. Size-->63330893.use. size-- >8000729do not use. size-->6663169

Do not wait until the performance bottleneck to optimize the code, but in the development of the appropriate consideration, compared to the performance bottlenecks to refactor these small details of the code, a good programming habit is particularly important.

Optimizing Java string connections and loops

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.