Z
zero
In a chess program I am working on I was using a HashSet to store objects
of class Pattern, which contained a HashSet of Pieces. During execution
there can be anywhere between 1 and several hundred thousand patterns in
the Set. I was using Sets because there should be no duplicates of Pieces
in a Pattern, and no duplicate Patterns in the set.
However, the first tests showed that the program was unacceptably slow, and
quickly threw OutOfMemoryExceptions. I soon found that the bottleneck was
the Set.add() method. I replaced each occurance of HashSet with ArrayList,
adding my own code to keep duplicates out. The result is a lot faster (I
can't give an estimate, but trust me it's *a lot*), and uses three times
less memory...
My reason for picking Sets was simply the elimination of duplicates, and
the constant time for add() sounded good too. Next time I'll think twice
before using a HashSet.
of class Pattern, which contained a HashSet of Pieces. During execution
there can be anywhere between 1 and several hundred thousand patterns in
the Set. I was using Sets because there should be no duplicates of Pieces
in a Pattern, and no duplicate Patterns in the set.
However, the first tests showed that the program was unacceptably slow, and
quickly threw OutOfMemoryExceptions. I soon found that the bottleneck was
the Set.add() method. I replaced each occurance of HashSet with ArrayList,
adding my own code to keep duplicates out. The result is a lot faster (I
can't give an estimate, but trust me it's *a lot*), and uses three times
less memory...
My reason for picking Sets was simply the elimination of duplicates, and
the constant time for add() sounded good too. Next time I'll think twice
before using a HashSet.