Daniel Pitts schrieb:
puzzlecracker wrote:
Item 25 of Effective Java 2nd Edition (Bloch): Prefer lists to arrays.
Lists are a higher level abstraction, and therefor "easier" to work
with.
and slower to work with...
Premature optimization is the root of all evil.
Lists are marginally slower to work with than arrays, but who cares if
your program is fast when it doesn't work correctly? Use the easiest
solution that works. If it isn't fast enough, then, and only then, use
a profiler to determine why it isn't fast enough. After that, and
*ONLY* after that, optimize.
Until now the only reason to optimize collections away for arrays was
never the speed but allways the RAM usage for me.
If you know that you will be holding about 100k to 1 Mio collections ..
then the overhead of the collections is enormous an using arrays becomes
a must.
ie
HashSet 1 obj holding an hashmap 12byte
HashMap 1 obj holding 8 Byte
3*int+1 float 16
entry array+map entrys 8+n*4 + n*Entry bytes
Entry: 8 Bytes
+ 3 object references 12 Byte
+ 1 int 4 Byte
so hashset:
44+ n*28 byte
(due to the load factor of the hasmap it would be even some bytes more..)
in comparison to just an array: 8+ n*4 bytes
Until now this 7 times overhead in space has allways been the killer
when used en masse.
Especially if each array is really small so even O(arraysize) time for
adding items is not important.
1) The discussion was List versus arrays not Set/Map versus arrays.
to Set/Map then that comparison also makes more sense.
array (*).