Overall content Introduction
This article is divided into several parts:
* Comparison of String, StringBuffer, StringBuilder
* Comparison of string stitching methods
What to use:
* JUNIT4 Unit Test
* SLF4J's log frame
Comparison of String, StringBuffer, StringBuilder
The scenarios used by these three are summarized as follows (see: "Writing a quality code: 151 recommendations for improving Java programs"):
-
String: String classes can be used in scenes where the strings do not change frequently, such as declarations of constants, small amounts of variable operations, and so on.
-
StringBuffer: In frequent string operations (stitching, replacing, deleting, etc.) and running in multithreaded environments, you might consider using StringBuffer, such as XML parsing, HTTP parameter parsing, and encapsulation.
-
StringBuilder: In frequent string operations (stitching, replacing, deleting, etc.) and running in multi-threaded environments, you might consider using StringBuffer, such as SQL statement Assembly, JSON encapsulation, and so on (it seems like these two I also use | StringBuffer).
string concatenation method Comparison
PackageCom.zy.util;ImportOrg.junit.Test;ImportOrg.slf4j.Logger;ImportOrg.slf4j.LoggerFactory; Public class teststring { Private FinalLogger Logger = Loggerfactory.getlogger ( This. GetClass ());@Test Public void Testplus() {String s ="";Longts = System.currenttimemillis (); for(inti =0; I <10000; i++) {s = s + string.valueof (i); }LongTe = System.currenttimemillis ();//system.out.println ("+ Cost {} MS:" + (Te-ts));Logger.info ("+ Cost {} MS", te-ts); }@Test Public void Testconcat() {String s ="";Longts = System.currenttimemillis (); for(inti =0; I <10000; i++) {s = S.concat (string.valueof (i)); }LongTe = System.currenttimemillis ();//system.out.println ("concat Cost {} MS:" + (Te-ts));Logger.info ("concat Cost {} MS", te-ts); }@Test Public void Teststringbuffer() {StringBuffer SB =NewStringBuffer ();Longts = System.currenttimemillis (); for(inti =0; I <10000; i++) {sb.append (string.valueof (i)); } sb.tostring ();LongTe = System.currenttimemillis ();//system.out.println ("StringBuffer Cost {} MS:" + (Te-ts));Logger.info ("StringBuffer Cost {} MS", te-ts); }@Test Public void Teststringbuilder() {StringBuilder SB =NewStringBuilder ();Longts = System.currenttimemillis (); for(inti =0; I <10000; i++) {sb.append (string.valueof (i)); } sb.tostring ();LongTe = System.currenttimemillis ();//system.out.println ("StringBuilder Cost {} MS:" + (Te-ts));Logger.info ("StringBuilder Cost {} MS", te-ts); }}
The results of the above procedure are as follows:
--6-9 -: on: - com. Zy. Util. TestStringTestplus Information: + Cost504Ms --6-9 -: on: - com. Zy. Util. TestStringTestconcat Information: concat cost196Ms --6-9 -: on: - com. Zy. Util. TestStringTeststringbuffer Information: StringBuffer cost2Ms --6-9 -: on: - com. Zy. Util. TestStringTeststringbuilder Information: StringBuilder cost2Ms
As you can see, the + and concat are slower and need to create a new string.
and StringBuffer and StringBuilder faster.
Reference Resources
- JUnit Unit Test: MU Lesson Network-Unit Test
- SLF4J Log Frame: Download introduction
- Reference Blog: 1 2 3
Insufficient place
- Use JAVAP to decompile, and then do the analysis
Unit tests for Java string concatenation