Collections API in ArcadeGame Programming?

Does the Collections API incur too much of a performance hit in arcade-style Java games? I really like the API but I have a sneaky suspicion that it is probably overkill for games where an array can do the job. Am I right? Or am I prematurely optimizing?

20thCB

At least ArrayList, LinkedList and HashMap are fine. I haven’t actually used others nearly at all. I wouldn’t suggest to use Iterators though.

LinkedList is definitely not OK if you want to frequently access stuff in the middle, which is an O(n) operation.

Don’t know if that was any use at all because most people already know it :-[

Cannot imagine anything more usefull and fast as HashMap/HashSet. Don’t know how I ever could live without…

Ok, in ancient C times I had an array of things and iterated over it when looking up things :frowning:

I don’t think collections are a performance problem. Used right, they can be a big performance gain!!!

Code using collections available in JDK (correct types of course). Inside class use specific types, but for inter-method communication use interfaces, so you can replace implementations easily.

When you will finishin your application, profile. You will probably find many other places to optimize. If after that, collections will prove to be a problem (most probably because need to creating wrappers for primitives for maps), switch to trove[1] in critical places and profile again.

Be sure to do that as late as possible - trove has very different performance characteristic than jdk collections. For example, trove hashmap memory usage is dependent only on size of it, while in JDK it is a strong function of number of values contained inside. Trove will always use only a few objects, where in JDK you will get at least one extra object per entry. Trove use different collision resolution, so depending on size of map and distribution of hashcodes, it can be few times faster or few times slower[2]
Generally, I have observed trove to be a bit faster and a bit less memory hungry for most cases, and a considerably cheaper garbage-wise for primitive->primitive mappings. I have used it mostly to avoid casting :wink:

[1] http://trove4j.sourceforge.net/
[2] Don’t believe benchmarks on trove page - they are designed to make trove win by wide marigin

[quote]LinkedList is definitely not OK if you want to frequently access stuff in the middle, which is an O(n) operation.

Don’t know if that was any use at all because most people already know it :-[
[/quote]
I mean’t that the Java implementation of LinkedList should be quite fast (though I’ve mostly used ArrayList). And you should use arraylist if you want random access.

No offense Inquisitor :wink:

No offense taken :slight_smile: