Performance and simplicity tradeoffs between String, StringBuffer, and StringBuilder

后端 未结 5 2198
轮回少年
轮回少年 2021-02-09 17:29

Have you ever thought about the implications of this change in the Java Programming Language?

The String class was conceived as an immutable class (and this decision was

5条回答
  •  生来不讨喜
    2021-02-09 17:55

    Decided to put the options to the test with a simple composition of XML exercise. Testing done on a 2.7GHz i5 with 16Gb DDR3 RAM for those wishing to replicate results.

    Code:

       private int testcount = 1000; 
       private int elementCount = 50000;
    
       public void testStringBuilder() {
    
        long total = 0;
        int counter = 0;
        while (counter++ < testcount) {
            total += doStringBuilder();
        }
        float f = (total/testcount)/1000;
        System.out.printf("StringBuilder build & output duration= %f µs%n%n", f); 
    }
    
    private long doStringBuilder(){
        long start = System.nanoTime();
        StringBuilder buffer = new StringBuilder("\n");
        buffer.append("");
          for (int i =0; i < elementCount; i++) {
              buffer.append("");
          }
          buffer.append("");
         //System.out.println(buffer.toString());
          output = buffer.toString();
          long end = System.nanoTime();
         return end - start;
    }
    
    
    public void testStringBuffer(){
        long total = 0;
        int counter = 0;
        while (counter++ < testcount) {
            total += doStringBuffer();
        }
        float f = (total/testcount)/1000;
    
        System.out.printf("StringBuffer build & output duration= %f µs%n%n", f); 
    }
    
    private long doStringBuffer(){
        long start = System.nanoTime();
        StringBuffer buffer = new StringBuffer("\n");
        buffer.append("");
          for (int i =0; i < elementCount; i++) {
              buffer.append("");
          }
          buffer.append("");
         //System.out.println(buffer.toString());
          output = buffer.toString();
    
          long end = System.nanoTime();
          return end - start;
    }
    

    Results:

    On OSX machine:
    
    StringBuilder build & output duration= 1047.000000 µs 
    
    StringBuffer build & output duration= 1844.000000 µs 
    
    
    On Win7 machine:
    StringBuilder build & output duration= 1869.000000 µs 
    
    StringBuffer build & output duration= 2122.000000 µs
    
    

    So looks like performance enhancement might be platform specific, dependant on how JVM implements synchronisation.

    References:

    Use of System.nanoTime() has been covered here -> Is System.nanoTime() completely useless? and here -> How do I time a method's execution in Java?.

    Source for StringBuilder & StringBuffer here -> http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Core/lang/java.lang.htm

    Good overview of synchronising here -> http://www.javaworld.com/javaworld/jw-07-1997/jw-07-hood.html?page=1

提交回复
热议问题