What’s the beginning of the lazy, straight to code

After thinking about it, I would like to give you a look at the photos of senior students, keep your eyes bright, code knock too much, hurt your eyes

1: Compare StringBuilder and String

@Test
public  void testString (a) {
    String s="";
    long begin = System.currentTimeMillis();
    for(int i=0; i<500000; i++){
        String s1 = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" ;
        String s2 = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE" ;
        String s3 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
        String s4 = s1+s2+s3;
    }
    long over = System.currentTimeMillis();
    System.out.println("Operation"+s.getClass().getName()+"The type is used for:"+(over-begin)+"毫秒");
}
@Test
public  void testStringBuilder (a) {
    StringBuilder sb = new StringBuilder();
    long begin = System.currentTimeMillis();
    for(int i=0; i<500000; i++){
        String s1 = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" ;
        String s2 = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE" ;
        String s3 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
        String s4= new StringBuilder(s1).append(s2).append(s3).toString();
    }// Join the Java Development Exchange jun sample: 756584822 to blow water chat
    long over = System.currentTimeMillis();
    System.out.println("Operation"+sb.getClass().getName()+"The type is used for:"+(over-begin)+"毫秒");
}
Copy the code

The operation time of the java.lang.StringBuilder type is 146 milliseconds. The operation time of the Java.lang.String type is 139 milliseconds

Change the testStringBuilder to

StringBuilder s4= new StringBuilder(s1).append(s2).append(s3);
Copy the code

The time to operate on the Java.lang.StringBuilder type is 110 milliseconds

As you can see, it’s faster and faster to just use s1 plus. It’s a little bit better when you don’t convert StringBuilder to String.

Reason: String + is essentially a StringBuilder operation, and JAVA is estimated to have done some optimization to make it faster than StringBuilder.

The conclusion is that for scenarios where multiple variables are conjoined at once, using String + is not bad, so if there is no need to convert to String, StringBuilder is better.

StringBuilder Multiple times of rotation String Connection Multiple times of rotation StringBuilder Multiple times of rotation

2: Multiple splicing scenes who is better

@Test
public  void testString (a) {
    String s="";
    long begin = System.currentTimeMillis();
    for(int i=0; i<500000; i++){
        String s1 = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" ;
        String s2 = s1 + "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE" ;
        String s3 = s2+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
    }
    long over = System.currentTimeMillis();
    System.out.println("Operation"+s.getClass().getName()+"The type is used for:"+(over-begin)+"毫秒");
}
@Test
public  void testStringBuilder (a) {
    StringBuilder sb = new StringBuilder();
    long begin = System.currentTimeMillis();
    for(int i=0; i<500000; i++){
        String s1 = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" ;
        StringBuilder s4= new StringBuilder(s1);
        String s2 = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE" ;
        s4.append(s2);
        String s3 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
        s4.append(s3);
       // s4.append(UUIDUtil.uuid());
    }
    long over = System.currentTimeMillis();
    System.out.println("Operation"+sb.getClass().getName()+"The type is used for:"+(over-begin)+"毫秒");
}
Copy the code

The operation time of the java.lang.StringBuilder type is 110 milliseconds. The operation time of the java.lang.String type is 205 milliseconds

As you can see, StringBuilder has far outperformed String in a discontinuous scenario.

Take StringBuilder one step further

@Test
public  void testStringBuilder (a) {
    StringBuilder sb = new StringBuilder();
    long begin = System.currentTimeMillis();
    for(int i=0; i<500000; i++){
        StringBuilder s4= new StringBuilder(300);
        String s1 = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" ;
        s4.append(s1);
        String s2 = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE" ;
        s4.append(s2);
        String s3 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
        s4.append(s3);
       // s4.append(UUIDUtil.uuid());
    }
    long over = System.currentTimeMillis();
    System.out.println("Operation"+sb.getClass().getName()+"The type is used for:"+(over-begin)+"毫秒");
}
Copy the code

The time to operate on the Java.lang.StringBuilder type is 57 milliseconds

You can improve The StringBuilder capability again by pre-sizing the StringBuilder (if you can predict it). The reason is that when StringBuilder doesn’t fit, it expands.

The operation that allows the StringBuilder capability to take off

@Test
public  void testStringBuilder (a) {
    StringBuilder sb = new StringBuilder();
    long begin = System.currentTimeMillis();
    StringBuilder s4= new StringBuilder(300);
    for(int i=0; i<500000; i++){
        s4.setLength(0);
        String s1 = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" ;
        s4.append(s1);
        String s2 = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE" ;
        s4.append(s2);
        String s3 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
        s4.append(s3);
       // s4.append(UUIDUtil.uuid());
    }// Join the Java Development Exchange jun sample: 756584822 to blow water chat
    long over = System.currentTimeMillis();
    System.out.println("Operation"+sb.getClass().getName()+"The type is used for:"+(over-begin)+"毫秒");
}
Copy the code

The time to operate on the Java.lang.StringBuilder type is 15 milliseconds

In the case of high performance requirements for string concatenation, the performance in descending order is as follows:

StringBuilder(memory overcommitment 15mm) > StringBuilder(predefined memory size 57) > StringBuilder(concatenation not to String 110) > String (continuous concatenation 139) > StringBuilder (Concatenation to String 146)

It really takes off here

    @Test
    public  void testString (a) {
        String s="";
        long begin = System.currentTimeMillis();
        for(int i=0; i<500000; i++){
            String s1 = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" +
            "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE" +
                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
                     // Join the Java Development Exchange jun sample: 756584822 to blow water chat
        }
        long over = System.currentTimeMillis();
        System.out.println("Operation"+s.getClass().getName()+"The type is used for:"+(over-begin)+"毫秒");
    }
Copy the code

The time to operate on the java.lang.String type is 0 milliseconds

This was already compiled to a String, without the append operation. If you have a performance requirement and you can use it, use it this way.

Finally, I wish you all an early success in your studies, a satisfactory offer, rapid promotion and salary increase, and the peak of your life.

Can please give me a three to support me?????? [Access to information]