What is the difference between using collections and arrays of primitives?
Consider Arrays with primitives for performance-critical processing by @marcinmoskala 👇
10/25/2021, 8:28 AM
While the general point is correct, the statement about the memory is not. The default list implementation - ArrayList uses array under the hood so there is no overhead in memory usage. The problem with performance comes from different source: automatic boxing.
Oh, I am not quite correct because even Array of Int uses boxing.
It is possible to do list that uses IntArray though. It will use the same amount of memory but still will be slower.
As for benchmarks, they strongly depend on JVM being used. Much better on GraalVM for such tasks and could be much worse on Android.
10/25/2021, 8:58 AM
As mentioned in the article in most cases still using of Collections is better choice, But for sure for some performance critical software arrays ma be good choice.
10/25/2021, 10:10 AM
(Heap)ByteBuffers, IntBuffers, etc. are also arrays with benefits. particularly ByteBuffers allow for primitive conversions to and from from unaligned bytes.