String str=str+ "I", StringBuffer str=stringbuffer.append (i) and StringBuilder str=stringbuilder.append (i) when a sequential string operation is encountered What difference does it have.
Think for two seconds ...
Use the procedure to verify it.
Code:
Package com.blueray.java.test;
public class Stringoperation {public
long Stringadd (long) {
long starttime=system.currenttimemillis ();
String s= "";
for (long i=0;i<times;i++) {
s=s+i;
}
Long Endtime=system.currenttimemillis ();
Long timebuf= endtime-starttime;
System.out.println (timebuf+ ",");
Return Timebuf
}
}
Execute code:
public class String_stringbuffer_compare {public static long times_fifty = 50L;
public static long Times_one_hundrod = 100L;
public static long Times_two_hundrod = 200L;
public static long Times_five_hundrod = 500L;
public static long Times_one_thousand = 1000L;
public static long Times_five_thousand = 5000L;
public static long Times_ten_thousand = 10000L;
public static long Times_fifty_thousand = 50000L;
public static long Times_hundrod_thousand = 100000L; public static long[] times = {times_fifty, Times_one_hundrod, Times_two_hundrod, Times_five_hundrod, Times_one_thousan
D, Times_five_thousand,times_ten_thousand,times_fifty_thousand,times_hundrod_thousand}; /** * @param args */public static void main (string[] args) {//for (long Time:times) {//Stringoperation Stro
PT = new Stringoperation ();
Stropt.stringadd (time); }//for (long Time:times) {//Stringbufferoperation stropt = new Stringbufferoperation ();//Stropt.stringapp
End (time);for (long time:times) {stringbuilderoperation stropt = new Stringbuilderoperation ();
Stropt.stringappend (time); }
}
Executed 4 times, results:
Times |
Times |
Times |
Times |
1000 times |
5000 times |
10000 times |
50000 times |
100000 times |
1 |
0 |
2 |
3 |
9 |
63 |
175 |
5170 |
24403 |
0 |
1 |
1 |
4 |
11 |
65 |
178 |
5232 |
24077 |
0 |
0 |
5 |
3 |
11 |
82 |
179 |
5266 |
24133 |
0 |
0 |
1 |
4 |
10 |
77 |
179 |
5367 |
24133 |
Stringconcatoperation:
Package com.blueray.java.test;
public class Stringconcatoperation {public
long Stringconcat (long) {
long starttime= System.currenttimemillis ();
String s= "";
for (long i=0;i<times;i++) {
s=s.concat (string.valueof (i));
}
Long Endtime=system.currenttimemillis ();
Long timebuf= endtime-starttime;
System.out.println (timebuf+ ",");
Return Timebuf
}
}
Execution results:
Times |
Times |
Times |
Times |
1000 times |
5000 times |
10000 times |
50000 times |
100000 times |
1 |
0 |
1 |
2 |
7 |
47 |
99 |
2699 |
12650 |
0 |
0 |
1 |
2 |
6 |
46 |
98 |
2679 |
12185 |
0 |
1 |
1 |
2 |
6 |
45 |
93 |
2666 |
12511 |
1 |
0 |
1 |
2 |
7 |
54 |
94 |
2669 |
12143 |
Stringbufferoperation:
Package com.blueray.java.test;
public class Stringbufferoperation {public
long stringappend (long) {
long starttime= System.currenttimemillis ();
StringBuffer s=new StringBuffer ();
for (long i=0;i<times;i++) {
s.append (i);
}
Long Endtime=system.currenttimemillis ();
Long timebuf= endtime-starttime;
System.out.println (timebuf+ ",");
Return Timebuf
}
}
Results:
Times |
Times |
Times |
Times |
1000 times |
5000 times |
10000 times |
50000 times |
100000 times |
0 |
0 |
0 |
1 |
1 |
11 |
12 |
12 |
8 |
0 |
0 |
0 |
1 |
1 |
11 |
23 |
15 |
9 |
0 |
0 |
0 |
0 |
1 |
13 |
24 |
15 |
10 |
0 |
0 |
0 |
0 |
1 |
17 |
12 |
11 |
11 |
Stringbuilderoperation:
Package com.blueray.java.test;
public class Stringbuilderoperation {public
long stringappend (long) {
long starttime= System.currenttimemillis ();
StringBuilder s=new StringBuilder ();
for (long i=0;i<times;i++) {
s.append (i);
}
Long Endtime=system.currenttimemillis ();
Long timebuf= endtime-starttime;
System.out.println (timebuf+ ",");
Return Timebuf
}
}
Results:
Times |
Times |
Times |
Times |
1000 times |
5000 times |
10000 times |
50000 times |
100000 times |
0 |
0 |
0 |
0 |
1 |
12 |
18 |
9 |
8 |
0 |
0 |
0 |
1 |
1 |
10 |
19 |
11 |
13 |
0 |
0 |
1 |
0 |
1 |
9 |
13 |
7 |
8 |
0 |
0 |
0 |
0 |
1 |
12 |
27 |
9 |
9 |
Analysis:
1. As can be seen from the graph, the efficiency of StringBuilder and StringBuffer is better than that of string
2. StringBuffer and StringBuilder, when the number of executions from 5000 to 10000, the execution time has increased significantly, StringBuilder time increase curve will be more steep, indicating StringBuilder Better than StringBuffer in a single-threaded environment.
Conclusion:
At execution time, the string is less efficient than two sb. It's better to use StringBuilder without taking into account multithreaded environments.
Insufficient:
(1). String accumulation is a string object type, while StringBuffer and StringBuilder add a long type, although you do not know the difference between stringbuffer.append add String and add long. But if you change to append (String), it might be fairer.
(2). No comparison string.concat.