Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 : String join operation has significant performance impact

Tags:

java

java-8

I was going through the newly added existing features introduced in Java-8. One simple feature newly added to String class is quiet appealing for me – that is String Join method.

Example:

String.join(" ", "AZY","BAX"); // returns AZY BAX

For curiosity, I have checked the performance (execution time) of this feature by writing a simple java code

public static void main(String[] args) {
    long start = System.nanoTime();
    String abc= String.join(" ,"AZY","BAX" … // joining 1000 words of size 3 char;
    long diff = System.nanoTime() - start;
    System.out.println(" Java 8 String Join " + diff);

     start = System.nanoTime();
    abc= "AZY"+"BAX"+"CBA"+ … // adding 1000 word of size 3 char;
    diff = System.nanoTime() - start;
    System.out.println(" Tranditional " + diff);

    start = System.nanoTime();
    new StringBuilder().append("AZY").append("BAX").appe… // appending 1000 word of size 3 char;
    diff = System.nanoTime() - start;
    System.out.println(" String Builder Append " + diff);

}

The result is not so exciting for me (time in neno sec)

Java 8 String Join     1340114
Tranditional             59785
String Builder Append   102807

The complexity is of o(n) – in-fact it is (n * Size of individual element length)

Other performance measures (memory etc) I have not measured.

My questions are:

  1. Is there anything wrong in my measurement (most of the time I believe on the jdk guys)
  2. What is the intent of adding “join” API to String class
  3. Is there any performance analysis for Java 8 is available
like image 506
dgm Avatar asked Jun 05 '14 06:06

dgm


People also ask

What is the use of Join () method in Java?

The java.lang.string.join () method concatenates the given element with the delimiter and returns the concatenated string. Note that if an element is null, then null is added. The join () method was introduced in java string since JDK 1.8.

How to join elements in a string in Java?

The join () method was introduced in java string since JDK 1.8. String.join () method takes two parameters first is a delimiter and the second can be a list or array of elements or elements separated by , (comma). String joined=String.join ("delimiter","element1","element2,"element3"...);

What's new in string class in java-8?

Bookmark this question. Show activity on this post. I was going through the newly added existing features introduced in Java-8. One simple feature newly added to String class is quiet appealing for me – that is String Join method. For curiosity, I have checked the performance (execution time) of this feature by writing a simple java code

Does the final keyword improve performance in Java?

The performance benefit of using the final keyword is a very popular debate topic among Java developers. Depending on where we apply it, the final keyword can have a different purpose and different performance implications.


1 Answers

First things first. This is not how you microbench Java

Read How do I write a correct micro-benchmark in Java? first. Your numbers are completely irrelevant, so lets ignore them.

Looking at the second example:

abc= "AZY"+"BAX"+"CBA"+...

These look like compile time constants to me. This String would be concatenated at compile time and there would be nothing to benchmark. This is a useless comparison as the whole point of the StringBuilder or String.join is to concatenate Strings that are not compile time constant.

Moving onto comparing the StringBuilder and String.join. Looking at the source code:

public static String join(CharSequence delimiter, CharSequence... elements) {
    Objects.requireNonNull(delimiter);
    Objects.requireNonNull(elements);
    // Number of elements not likely worth Arrays.stream overhead.
    StringJoiner joiner = new StringJoiner(delimiter);
    for (CharSequence cs: elements) {
        joiner.add(cs);
    }
    return joiner.toString();
}

This uses a StringJoiner. A StringJoiner simply uses a StringBuilder under the hood, so the two are equivalent.

It is often much more informative to look at the code than to try and benchmark performance. Even if you do benchmark correctly.

It's also worth noting that your first method, with join, joins the 1000 Strings on " " (space). Whereas your StringBuilder method simply appends them together. These two are not the same.

The point of the String.join method is that you can do:

String.join(", ", "a", "b", "c") // result is "a, b, c"

With a StringBuilder you would have to add a lot more code.

like image 179
Boris the Spider Avatar answered Sep 24 '22 23:09

Boris the Spider