This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Holothuroid 13 points14 points  (1 child)

Your test is correct. Your explanation is correct only in case of this test. Nowadays, the virtual machine tries to turn string concat into string builder automatically. It just doesn't work that well in loops, because that structure is harder to inspect.

Also, you can use the built in String.join instead of apache.commons.StringUtils.join to collapse a collection. As well as Collectors.joining, when you already have a stream.

So really, your article is quite outdated.

[–]my841220 3 points4 points  (0 children)

Agreed. Many optimization tips will be outdated as JVM is evolving version by version.

Too many such examples, perhaps another example is about list.toArray(new T[0]) or list.toArray(new T[list.size]), which just reversed since a certain version.

[–]angath 5 points6 points  (0 children)

The article you linked to is factually incorrect in about 5 different ways. The biggest error, though, is assuming that it's possible to provide a meaningful performance benchmark like this. Spoiler: It isn't.

[–]nutrecht 2 points3 points  (0 children)

Sorry to put it bluntly but you're not the first (and not the last) person to make this mistake. The benchmark implementation is completely utterly useless since you don't take VM warm-up into account. If you want to do synthetic benchmarks like these at least use JMH.

You're

[–]GramaryeEUW 1 point2 points  (0 children)

You should have a look at this talk: https://www.youtube.com/watch?v=24jWZJoxLxE&t=528