📜 ⬆️ ⬇️

Java string classes. Performance comparison

Have you ever wondered how exactly the performance of Java string classes differs?

In this topic I tried to compare the performance of java.lang classes String, StringBuilder and StringBuffer.

Obvious facts

It's no secret that in Java there are three main classes for working with strings. The main class that we most often use in programs is String . A feature of this class is that it creates immutable strings. Those. what character string we initialized the object to create, so it will remain. Therefore, the design:

String st = "";
st += "";

Create a new object containing the string "MashaSasha" and the original objects will be destroyed by the garbage collector.

If the concatenation operation on the same string object is performed a lot, this leads to an intensive process of generating new objects and adds work to the garbage collector.

If we need mutable strings, the developers offer us other classes. The first one, which was originally in Java, is StringBuffer and the newer StringBuilder (appeared since version 1.5). As written in the documentation, StringBuffer is safe to use in multi-threaded applications, but the second one is more efficient.

What do you want

Well, more effective, less effective, this is certainly good, but I want numbers. After all, another programmer might think, is it worth using "inconvenient" StringBuffer / StringBuilder instead of such a wonderful String in order to save a couple of milliseconds? Others will say that there are no such situations when you need to do well, say, a hundred thousand concatenations ... But, curiously, is there a big difference?


We read lines from the text file “onegin.txt” (let's call for the help of a classic). The file has a size of 297463 bytes, utf-8, 27195 words. Add all the words of the file in one line using all three classes and compare the performance. That it was more interesting, we will carry out the test on different JVM and in two OS. Linux (I use LinuxMint 9 is such an adorned Ubuntu, if anyone does not know) and Win XP Pro SP3. Both OSes are 32-bit, because I write from a netbook with an Atom N280. But we are not setting records, the trend is important to us.

Actually the program itself, nowhere easier:
package stringtest1;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Main {

public static void main( String [] args) throws FileNotFoundException, IOException {

BufferedReader reader = new BufferedReader( new FileReader( "onegin.txt" ));

StringBuilder sb = new StringBuilder ();
String line = null ;
while ( (line = reader.readLine()) != null ) {
sb.append(line).append( "\n" );
String [] words = sb.toString().split( "\\s+" );
System. out .println( "Total words:" + words.length);

long ts = System.nanoTime();

String buff = "" ;
//2 StringBuffer buff = new StringBuffer();
//3 StringBuilder buff = new StringBuilder();

for ( String word : words) {
buff += word + " " ;
//2&3 buff.append(word).append(" ");

long te =System.nanoTime();

System. out .println( "Complete, lenght:" + buff.length() + " elapsed time:" + (te - ts)/1e6 + "ms" );


private static void waitEnter() {
Scanner scan = new Scanner(System. in );
System. out .print( "Press Enter key." );


* This source code was highlighted with Source Code Highlighter .

The options for StringBuffer and StringBuilder are similar, and are displayed in the comments.
Measurements of the operation time of each variant for each virtual machine were made 5 times and the average value was calculated.



ClassOpen JDK 1.6.0_18HotSpot 1.6.0_20JRockit 4.0.1

Windows xp

ClassHotSpot 1.6.0_20JRockit 4.0.1


If we need modifiable strings, we use StringBuffer (in multi-threaded applications) and StringBuilder in the rest. The difference is quite palpable.

It is clear why the JVK is a little behind the Open JDK, something Sun is there or has already been turned on by Oracle. It's not entirely clear why such a big difference in working with objects of the String class under Linux and WinXP. Winning toli does not allow JVM to work as efficiently as Linux, or it is a peculiarity of the JVM implementation ... Then you need to dig further.

For reference:
JRockit is a virtual machine from Oracle.
HotSpot is a machine originally developed by Sun, now by itself purchased by Oracle.
Open JDK - Open-source JDK based on source code in its time open Sun-ohm.

Source: https://habr.com/ru/post/102468/

All Articles