string stringbuffer stringbuilder效率比较

时间:2022-10-13 03:56:54

代码

package com.eblly;

import org.junit.Test;

import java.math.BigDecimal;

/**
 * Created by eblly on 2017/5/11.
 */
public class StringTs {

    @Test
    public void test2() {

        long   startLong = System.currentTimeMillis();
        String str       = new String();

        for (int i = 0; i <= 10000; i++) {
            str = str + "3333";
        }

        System.out.println(System.currentTimeMillis() - startLong); // 403

        //==================================================================
        startLong = System.currentTimeMillis();
        String str2 = new String();

        for (int i = 0; i <= 10000; i++) {
            StringBuilder stringBuilder = new StringBuilder();
            String        tmpStr        = "2222";
            stringBuilder.append(str2).append(tmpStr);
            str2 = stringBuilder.toString();
        }

        System.out.println(System.currentTimeMillis() - startLong); // 337

        //==================================================================
        startLong = System.currentTimeMillis();
        StringBuffer strBuff = new StringBuffer();

        for (int i = 0; i <= 10000; i++) {
            strBuff.append("4444");
        }

        System.out.println(System.currentTimeMillis() - startLong); // 1

        //==================================================================
        startLong = System.currentTimeMillis();
        StringBuilder strBuilder = new StringBuilder();

        for (int i = 0; i <= 10000; i++) {
            strBuilder.append("5555");
        }

        System.out.println(System.currentTimeMillis() - startLong); // 1


//        System.out.println(str.length());
//        System.out.println(str2.length());

        System.out.println();
    }

}

运行结果:

388
310
0
1

3333和2222和运行速度差不多。

使用javap -c反编译查看代码。

Compiled from "StringTs.java"
public class com.eblly.StringTs {
  public com.eblly.StringTs();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public void test2();
    Code:
       0: invokestatic  #2                  // Method java/lang/System.currentTimeMillis:()J
       3: lstore_1
       4: new           #3                  // class java/lang/String
       7: dup
       8: invokespecial #4                  // Method java/lang/String."<init>":()V
      11: astore_3
      12: iconst_0
      13: istore        4
      15: iload         4
      17: sipush        10000
      20: if_icmpgt     49
      23: new           #5                  // class java/lang/StringBuilder
      26: dup
      27: invokespecial #6                  // Method java/lang/StringBuilder."<init>":()V
      30: aload_3
      31: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      34: ldc           #8                  // String 3333
      36: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      39: invokevirtual #9                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      42: astore_3
      43: iinc          4, 1
      46: goto          15
      49: getstatic     #10                 // Field java/lang/System.out:Ljava/io/PrintStream;
      52: invokestatic  #2                  // Method java/lang/System.currentTimeMillis:()J
      55: lload_1
      56: lsub
      57: invokevirtual #11                 // Method java/io/PrintStream.println:(J)V
      60: invokestatic  #2                  // Method java/lang/System.currentTimeMillis:()J
      63: lstore_1
      64: new           #3                  // class java/lang/String
      67: dup
      68: invokespecial #4                  // Method java/lang/String."<init>":()V
      71: astore        4
      73: iconst_0
      74: istore        5
      76: iload         5
      78: sipush        10000
      81: if_icmpgt     123
      84: new           #5                  // class java/lang/StringBuilder
      87: dup
      88: invokespecial #6                  // Method java/lang/StringBuilder."<init>":()V
      91: astore        6
      93: ldc           #12                 // String 2222
      95: astore        7
      97: aload         6
      99: aload         4
     101: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     104: aload         7
     106: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     109: pop
     110: aload         6
     112: invokevirtual #9                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     115: astore        4
     117: iinc          5, 1
     120: goto          76
     123: getstatic     #10                 // Field java/lang/System.out:Ljava/io/PrintStream;
     126: invokestatic  #2                  // Method java/lang/System.currentTimeMillis:()J
     129: lload_1
     130: lsub
     131: invokevirtual #11                 // Method java/io/PrintStream.println:(J)V
     134: invokestatic  #2                  // Method java/lang/System.currentTimeMillis:()J
     137: lstore_1
     138: new           #13                 // class java/lang/StringBuffer
     141: dup
     142: invokespecial #14                 // Method java/lang/StringBuffer."<init>":()V
     145: astore        5
     147: iconst_0
     148: istore        6
     150: iload         6
     152: sipush        10000
     155: if_icmpgt     172
     158: aload         5
     160: ldc           #15                 // String 4444
     162: invokevirtual #16                 // Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
     165: pop
     166: iinc          6, 1
     169: goto          150
     172: getstatic     #10                 // Field java/lang/System.out:Ljava/io/PrintStream;
     175: invokestatic  #2                  // Method java/lang/System.currentTimeMillis:()J
     178: lload_1
     179: lsub
     180: invokevirtual #11                 // Method java/io/PrintStream.println:(J)V
     183: invokestatic  #2                  // Method java/lang/System.currentTimeMillis:()J
     186: lstore_1
     187: new           #5                  // class java/lang/StringBuilder
     190: dup
     191: invokespecial #6                  // Method java/lang/StringBuilder."<init>":()V
     194: astore        6
     196: iconst_0
     197: istore        7
     199: iload         7
     201: sipush        10000
     204: if_icmpgt     221
     207: aload         6
     209: ldc           #17                 // String 5555
     211: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     214: pop
     215: iinc          7, 1
     218: goto          199
     221: getstatic     #10                 // Field java/lang/System.out:Ljava/io/PrintStream;
     224: invokestatic  #2                  // Method java/lang/System.currentTimeMillis:()J
     227: lload_1
     228: lsub
     229: invokevirtual #11                 // Method java/io/PrintStream.println:(J)V
     232: getstatic     #10                 // Field java/lang/System.out:Ljava/io/PrintStream;
     235: invokevirtual #18                 // Method java/io/PrintStream.println:()V
     238: return
}

其中

 17: sipush        10000
      20: if_icmpgt     49
      23: new           #5                  // class java/lang/StringBuilder
      26: dup
      27: invokespecial #6                  // Method java/lang/StringBuilder."<init>":()V
      30: aload_3
      31: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      34: ldc           #8                  // String 3333
      36: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      39: invokevirtual #9                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      42: astore_3

 78: sipush        10000
      81: if_icmpgt     123
      84: new           #5                  // class java/lang/StringBuilder
      87: dup
      88: invokespecial #6                  // Method java/lang/StringBuilder."<init>":()V
      91: astore        6
      93: ldc           #12                 // String 2222
      95: astore        7
      97: aload         6
      99: aload         4
     101: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     104: aload         7
     106: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     109: pop
     110: aload         6
     112: invokevirtual #9                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     115: astore        4

类似。
只不过后者重排序的结果不一样,运行效率相近。
String str += "3333";会先创建StringBuilder再使用append()拼接字符串,之后再toString()返回给String;如果String需要拼接多次,则会创建多个StringBuilder,调用多次toString()。因此效率比较低。

StringBuffer和StringBuilder效率相近。