Why StringBuilder is much faster than String

谁说胖子不能爱 提交于 2020-06-11 05:14:12

问题


I know that there are a lot of similar questions posted here, but these don't really answer my question.

I just wanted to know why StringBuilder is much faster than string concatenation using the + operator. Even though that the + operator internally is implemented using either StringBuffer or StringBuilder?

public void shortConcatenation(){
    long startTime = System.currentTimeMillis();
    while (System.currentTimeMillis()-startTime<=1000){


        character+="Y";

    }

    System.out.println("short :"+character.length());
}

//// using String builder

 public void shortConcatenation2(){
    long startTime = System.currentTimeMillis();
    StringBuilder sb = new StringBuilder();
    while (System.currentTimeMillis()-startTime<=1000){

        sb.append("Y");
    }
    System.out.println("string builder short :"+sb.length());
}

Thanks in advance!


回答1:


Do you understand how it works internally?

Every time you do stringA += stringB; a new string is created an assigned to stringA so it will consume memory (new string instance!) and time (copy the old string + new chars of the other string).

StringBuilder will use internally an array of chars and when you use .append() method it will do several things:

  • check if there are any free space for the string to append
  • again some internal checks and run a System.arraycopy to copy the chars of the string in the array.

Personally, i think the allocation of a new string every time (creating a new instance of string, put the string etc.) could be very expansive in terms of memory and speed (in while/for etc. especially).

In your example, use a StringBuilder is better but if you need (example) something simple like a .toString()

public String toString() {
    return StringA + " - " + StringB;
}

makes no differences (well, in this case is better you avoid StringBuilder overhead which is useless here).




回答2:


Strings in java are immutable. This means that methods that operate on strings cannot ever change the value of a string. String concatenation using += works by allocating memory for an entirely new string that is the concatenation of the 2 previous ones, and replacing the reference with this new string. Each new concatenation requires the construction of an entirely new String object.

In constrast, the StringBuilder and StringBuffer classes are implemented as a mutable sequence of characters. This means that as you append new Strings or chars onto a StringBuilder, it simply updates its internal array to reflect the changes you've made. This means that new memory is only allocated when the string grows past the buffer already existing in a StringBuilder.




回答3:


I can list a very nice example for understanding the same(I mean I felt its a nice example), check the code here taken from leetcode Problem: https://leetcode.com/problems/remove-outermost-parentheses/ 1: using String public String removeOuterParentheses(String S) {

    String a="";
    int num=0;
    for(int i=0;i<S.length()-1;i++) {
     if(S.charAt(i)=='(' && num++>0) {
         a+="(";

     } 
        if(S.charAt(i)==')' && num-->1) {
         a+=")";

     } 

    }
  return a;  
}

and now, using StringBuilder. public String removeOuterParentheses(String S) {

    StringBuilder sb = new StringBuilder();


    int a=0;
    for(char ch : S.toCharArray()) {
        if(ch=='(' && a++>0) sb.append('(');
        if(ch==')' && a-->1) sb.append(')');
    }

    return sb.toString();

}

The performance of both varies by a huge margin, The first submission uses String while the latter one uses StringBuilder.

As explained above the theory is the same. String by property is immutable and synchronous,i.e. its state cannot be changed. The second eg. is expensive owing to the creation of a new memory allocation whenever a concatenation function or "+" is used. It will consume a lot of heap and in return be slower. In comparison StringBuilder is mutable, it will only append and not create an overload on the memory consumed.

Hope this helps.



来源:https://stackoverflow.com/questions/22439177/why-stringbuilder-is-much-faster-than-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!