I wrote a program that reads numbers out of a file (about 500,000 of them), and inserts them to a data structure. the numbers are distinct.
I'm inserting the numbers to an unordered_map
with another struct (using std::make_pair(myNumber, emptyStruct))
.
And after the insertion of all the numbers, I'm using it to search only a couple of hundred times. I never delete the DS until the program finish running.
After profiling, I've noticed that the insert operation takes about 50% of the running time. (There is also some other code, that runs as many times as the insertion, but it doesn't take so much time).
I thought maybe the resizing takes time, so I used the reserve function with 500,000, but the results are still the same.
As far as I know, this DS should be O(1) insert and search (and the trade off is large memory), so I don't see why it takes so much time to insert. How can I improve my results?