Java performance trap: is the performance of StringBuffer really better than that of String?

Source: Internet
Author: User

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:

  1. Public ClassStringTest {
  2. Public Static VoidMain (String[] Args ){
  3. LongStart =System. CurrentTimeMillis ();
  4. For(IntI = 0; I <10000; I ++ ){
  5. StringS = "This is a" + "long test string for" + "different JDK performance" + "testing .";
  6. }
  7. LongEnd =System. CurrentTimeMillis ();
  8. System. Out. println ("Directly string contact:" + (end-start ));
  9. Start =System. CurrentTimeMillis ();
  10. For(IntI = 0; I <10000; I ++ ){
  11. StringBufferBuffer =New StringBuffer();
  12. Buffer. append ("This is ");
  13. Buffer. append ("long test string ");
  14. Buffer. append ("different JDK performance ");
  15. Buffer. append ("testing .");
  16. StringSs = buffer. toString ();
  17. }
  18. End =System. CurrentTimeMillis ();
  19. System. Out. println ("StringBuffer contact:" + (end-start ));
  20. }
  21. }

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
  1. Public ClassStringTest {
  2. Public Static VoidMain (String[] Args ){
  3. StringS1 = "This is ";
  4. StringS2 = "long test string ";
  5. StringS3 = "different JDK performance ";
  6. StringS4 = "testing .";
  7. LongStart =System. CurrentTimeMillis ();
  8. For(IntI = 0; I <10000; I ++ ){
  9. StringS = s1 + s2 + s3 + s4;
  10. }
  11. LongEnd =System. CurrentTimeMillis ();
  12. Html "target =" _ blank "> < p><>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.