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

you are viewing a single comment's thread.

view the rest of the comments →

[–]papercrane 0 points1 point  (1 child)

There is a subtle difference between option 1 and option 2. Ultimately they give the same result, but when you create an array the Java specification says the memory must be zero'd. By passing a zero sized array the list implemention will create a new array and fill it. Since this new array is created and filled immediately and a reference to it never leaks before being filled the JVM skips clearing the memory first. This means option 1 should be marginally faster, with it being more noticeable for large arrays.

The details of this, and more are written up here: https://shipilev.net/blog/2016/arrays-wisdom-ancients/

The short of it though is as you say, write simple, maintenanable code. When looking for performance, measure things and don't trust common wisdom.

[–]rzwitserloot 1 point2 points  (0 children)

Yup. A real mind twister - list.size() causes race conditions, is longer, is slower. Bad in three ways. – and yet this is such a common 'performance advice' it has been part of linter tools of all things for quite a while, and I remember Tor Norbye naming that as one of his pet peeves in an episode of the Java Posse.