I am writing a program which accepts 400 numbers of type long and will modify some of them depending on conditions at runtime and I want to know whether to use ArrayList<Long> or long[].
Which will be faster to use? I am thinking of using long[] because size is fixed.
When the size is fixed,
long[]is faster, but it allows a less maintainable API, because it does not implement theListinterface.Note a
long[]is faster for 2 reasons:longs and not box objectLongs (also enables better cache performace, since thelongs are allocated contigously and theLongs aren’t guaranteed to)Nevertheless, for simpler maintainability – I would have used a
List<Long>, unless performace is very critical at this part of the program.If you use this collection very often in a tight loop – and your profiler says it is indeed a bottleneck – I would then switch to a more efficient
long[].