| Comparison of String vs StringBuffer in java |
Quick test of String vs StringBuffer performanceI was curious about just how much of a difference using StringBuffer would be over using just the normal String functions. So anyway, here are my results: The first test looked at a scenario where StringBuffer should win easily. All that was required was to start with a value of "A", and then append 10000 single character strings to the end of it. The StringBuffer was just over three times quicker than a pure String implementation. StringAppend took 2667 milliseconds The second test created 10000 new String objects, and used the same StringBuffer object to replace the value of the existing StringBuffer with a new one. Here both implementations were fairly similiar in time. StringNew took 0 milliseconds The last test was to start with a 10 charactr String and replace 5 characters in that String and repeat the process 10000 times. Here both implementations were fairly similiar in time. StringReplace5Chars took 113 milliseconds Overall the StringBuffer implementations were slightly quicker, but not by much. Unless your program is appending large amounts of strings, using either implentation will result in very similar performance. The test was conducted on Ubuntu Linux using kernal 2.6.15-27-386 and using Sun Java 1.5.06 on an AMD Athlon(tm) 64 Processor 3000+. Download the source code I used for the test from here: http://www.about280.com/StringTest.java |
Comparison of String vs StringBuffer in java