GGirotto
GGirotto

Reputation: 1105

Why algorithm perfomance get worst with HashSet.add?

I'm working with an algorithm that has to read a file with 1 million lines and store some informations about this file. I found the HashSet structure that adds, remove and finds any data in O(1) performance. But, when i execute the algorithm with the line that add the data into the HashSet, the algorithm execution time became more than 4x worst. The HashSet performance become worst when we insert too many data into it?

Upvotes: 0

Views: 19

Answers (1)

Matzi
Matzi

Reputation: 13925

Different HashSet implementations can vary on performance. First of all there is a need for either some kind of a tree or a set of buckets, both has it's own performance cost. Theoratically the hash datastructures are fast, but reality can be much different. Even O(1) means that the execution time is independent of the number of elements, but it does not mean it's free or fast.

Upvotes: 1

Related Questions