Java 8 : String join operation has significant performance impact

java, java-8

Solution

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 `String`s 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 `String`s 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.

Problem

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: - Is there anything wrong in my measurement (most of the time I believe on the jdk guys) - What is the intent of adding “join” API to String class - Is there any performance analysis for Java 8 is available

Original source

Related problems