Have you read the String or StringBuffer written by cherami? As well as comments from netizens later, I feel it is necessary to thoroughly investigate this issue. Therefore, I wrote a simple test class and a script to run it. By modifying the test class to different parameters, and testing on different JDK, we found this problem really interesting. Let's get started.
Step 1: Prepare
To facilitate subsequent testing, it is necessary to write a simple script:
Echo test by jdk1.2.2
/Opt/java/jdk1.2.2/bin/javac StringTest. java
/Opt/java/jdk1.2.2/bin/java StringTest
Echo test by jdk1.3.1 _ 09
/Opt/java/jdk1.3.1 _ 09/bin/javac StringTest. java
/Opt/java/jdk1.3.1 _ 09/bin/java StringTest
Echo test by jdk1.4.2
/Opt/java/jdk1.4.2/bin/javac StringTest. java
/Opt/java/jdk1.4.2/bin/java StringTest
The above script can be applied on windows or linux as needed. I tested it in linux, so I saved it as a file stringtest. sh. If you test it on windows, you can save it as stringtest. bat.
Meaning: The running results after this article are continuously run for multiple times and take one of them to compare the average and typical sample values)
Step 2: Start writing code
At first, I wrote the following code without much consideration:
- Public ClassStringTest {
- Public Static VoidMain (String[] Args ){
- LongStart =System. CurrentTimeMillis ();
- For(IntI = 0; I <10000; I ++ ){
- StringS = "This is a" + "long test string for" + "different JDK performance" + "testing .";
- }
- LongEnd =System. CurrentTimeMillis ();
- System. Out. println ("Directly string contact:" + (end-start ));
- Start =System. CurrentTimeMillis ();
- For(IntI = 0; I <10000; I ++ ){
- StringBufferBuffer =New StringBuffer();
- Buffer. append ("This is ");
- Buffer. append ("long test string ");
- Buffer. append ("different JDK performance ");
- Buffer. append ("testing .");
- StringSs = buffer. toString ();
- }
- End =System. CurrentTimeMillis ();
- System. Out. println ("StringBuffer contact:" + (end-start ));
- }
- }
Running result:
Test by jdk1.2.2
Directly string contact: 0
StringBuffer contacts: 120
Test by jdk1.3.1 _ 09
Directly string contact: 1
StringBuffer contact: 47
Test by jdk1.4.2
Directly string contact: 0
StringBuffer contact: 53
Oh, is it a big accident ?!!! I started to do the same, but don't worry. In fact, I made a mistake. The Compiler optimized the compilation because strings + operations are directly measured by strings, so String s = "This is a" + "long test string for" + "different JDK performance" + "testing. "; after compilation, it actually becomes: String s =" This is a long test string for different JDK performance testing. ";, haha, this is a simple assignment operation, it is no wonder that the time is almost no longer used.
Step 3: modify the code
- Public ClassStringTest {
- Public Static VoidMain (String[] Args ){
- StringS1 = "This is ";
- StringS2 = "long test string ";
- StringS3 = "different JDK performance ";
- StringS4 = "testing .";
- LongStart =System. CurrentTimeMillis ();
- For(IntI = 0; I <10000; I ++ ){
- StringS = s1 + s2 + s3 + s4;
- }
- LongEnd =System. CurrentTimeMillis ();
- Html "target =" _ blank "> < p><>