The StringBuilder does not provide a clear or empty method.
There are 3 ways to clear:
1) Reborn into one, the old by the system itself voluntarily recycled
2) Use Delete
3) Use SetLength
Cycle three methods 10 million times, code:
1.public class SBBM {
2.
3. Static String A;
4. Static long time;
5. public static void Main (string[] args) throws Exception {
6.
7. StringBuilder sb = new StringBuilder ();
8. StringBuilder SB3 = new StringBuilder ();
9.
Ten. Time = System.currenttimemillis ();
One. for (int i = 0; i < 10000000; i++) {
StringBuilder SB2 = new StringBuilder ();
Sb2.append ("somestr6ing");
Sb2.append ("Somes5tring2");
Sb2.append ("some3strin4g");
Sb2.append ("so3mestr5ing");
Sb2.append ("so2mest7ring");
A = Sb2.tostring ();
19.}
System.out.println ("way2=" + (System.currenttimemillis ()-time));
21st.
22.
Time = System.currenttimemillis ();
(int i = 0; i < 10000000; i++) {
sb.delete (0, Sb.length ());
Sb.append ("somestring");
Sb.append ("SomeString2");
Sb.append ("somestrin4g");
Sb.append ("somestr5ing");
Sb.append ("somest7ring");
A = Sb.tostring ();
32.}
System.out.println ("way1=" + (System.currenttimemillis ()-time));
34.
Time = System.currenttimemillis ();
(int i = 0; i < 10000000; i++) {
37.
sb3.setlength (0);
Sb3.append ("somestr55ing");
Sb3.append ("Some44string2");
Sb3.append ("som55estrin4g");
Sb3.append ("some66str5ing");
Sb3.append ("so33mest7ring");
A= sb3.tostring ();
45.}
System.out.println ("way3=" + (System.currenttimemillis ()-time));
47.
48.
49.}
50.}
Note that the append string is not the same, otherwise the result will be affected by the Java string pool (i.e. 3 better than 2)
Results:
1.way2=9438
2.way1=6281
3.way3=6469
Ability to repeat the methods several times, point to order, etc.
Overall: Method 2 is better than Method 3 better than Method 1
In Java, the StringBuilder method of emptying is relatively