When looking at the average insertion times in an std::unordered_map, I saw that the insertion times varied very widely. Here is my code:
int main(){
unordered_map <int, int> m;
for(int i = 0; i < 200000; ++i){
double start = clock();
m[i] = i;
double time = (clock()-start)/(CLOCKS_PER_SEC/1000);
if(time >= 1){
cout << i << endl;
}
}
return 0;
}
Every time I run this code, I receive the exact same output:
12982
26266
53200
107896
Every other iteration runs in less than .01 milliseconds.
Now, all the books/websites I've read have said that inserting to a std::unordered_map runs in O(1) time, so why do the times vary so widely here? Is it something to do with the hash function or the size of the map?
Aucun commentaire:
Enregistrer un commentaire