### Arpa's blog

By Arpa, history, 4 years ago, ,

UPD: Tricks to make unordered_map faster added.

Hi!

##### What is unordred_map?

It is a data structure like map but it is more than 4 times faster than map.you can use it in C++11 with including #include<unordered_map>.for example:

#include<unordered_map>
using namespace std;
int main(){
unordered_map<int,int>mp;
mp[5]=12;
mp[4]=14;
cout<<mp[5]<<' '<<mp[4]<<endl;//prints: 12 14
}


Lets explain it more.

##### How it works?

Focus on unordered_set for simplify.You can imagine that it has vector of vector like vector<vector<type> > mp. Every time you insert value V in that, it calculate its hash(I will explain how it works), let hash(V)=K; it inserts V into mp[K] (formally it calls mp[K].push_back(V)).When you call mp.count(V) it searchs for V in mp[K].

##### map VS unordered_map (and set VS unordered_set)

1-unordered_map is more than 4 times faster

Focus on problem 527E - Data Center Drama, it seems that it is good to use unordered_map instead of map.

My submission with map: 14269000 Time:484 MS.

My submission with unordered_map: 14269381 Time:358 MS.

Another good sample to show how fast is unordered_map is problem 178C3 - Smart Beaver and Resolving Collisions:

My submission with map: 15781810 Time limit exceeded on test 36 .

My submission with unordered_map: 15774685 Accepted (Time:966 MS).

2-unordered_set (and unordered_map) is not sorted

Please note that set (and map) is sorted, for example *(s.begin()) is always smallest number in the set; but in unordered_set it isn't. similarly there isn't lower_bound and upper_bound in unordered_set (and unordered_map similarly).

##### Creating hash function for structs

Now, one other problem remains, you can try to compile this code:

unordered_map<pair<int,int>,int>mp;


You will get Compilation Error! Why? See this page for unordered_map supported types. For unsupported types, you have to create your own hash function for use. For example lets see how we can create a hash function for pair<int,int>.

As you know any int value is between -2^31+1 to 2^31-1.so if we create function that for every pair<int,int> returns distinct value in type size_t(alias of unsigned int), it will be done. It is pretty easy: x.first^(x.second<<32) is good. but be careful about overflow ;for having good hash function we use hash<long long>.The code is looks like this:

struct HASH{
size_t operator()(const pair<int,int>&x)const{
return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
}
};
unordered_map<pair<int,int>,int,HASH>mp;


Now you have a unordered_map of pair<int,int> (it isn't problem what is second member, in example it was int).Creating hash function for other structs is same.

##### How to test unordered_map is faster than map?

You can test that for N=10^6, unordered_set(unordered_map) is more than 4 times faster than set(map) ;with this code:(compile code with command: g++ file.cpp -std=c++11 -O2)

#include <bits/stdc++.h>
using namespace std;
unordered_set<int>s;//replace it with set for test.
int main(){
auto T=clock();
s.reserve(32768); //updated !
for(int i=0;i<1000000;i++)
s.insert(i);
cout<<double(clock()-T)/CLOCKS_PER_SEC<<'\n';
}


Note1: Let your hash function H(V), it is better that H(V) returns distinct value for every distinct V, it makes unordered_map faster; but if you can't do that it doesn't problem. The only problem is that unordered_map becomes slower(however it is better than map).

Note2: Please be careful about your hash function time complexly! it is fun to use this hash function:

struct HASH{
size_t operator()(const pair<int,int>&x)const{
size_t ans=0;
for(int i=0;i<x.first;i++)
ans+=x.second;
return ans;
}
};


UPD I will explain hash<type> in my next post.

UPD It seems that sometimes unordered_map becames so slow.but it can improve with this two lines of code:

unordered_map<int,int>mp;
mp.reserve(1024);


With this two lines unordered_map become about 10 times faster. You can replace 1024 with another suitable power of two.(it depends on number of insert s you will do).

• +47

 » 4 years ago, # |   0 Thank you so much! I had no idea about this. From now I'll use it as much as possible :)
•  » » 4 years ago, # ^ | ← Rev. 2 →   -6 you can use unordered_set/map when you don't need order of members.for example one of unordered_set applications is when you are storing all of the settings of one vector (for example); you can store them in unordered_set.for example: unordered_set >all; int count(vectorv){ if(all.count(v)) return 0; all.insert(v); ... } you will never check the same vectors with that trick!
 » 4 years ago, # |   0 Unordered_map isn't faster than map when count of elements isn't much. Sorry for bad eng :)
•  » » 4 years ago, # ^ | ← Rev. 2 →   +1 if N is about 100 ;map has equal time with unordred_map.but you can test it when N is about 10^6. it seems that unordered_map is about 4 times faster than map.
 » 4 years ago, # |   +28  size_t operator()(const pair&x)const{ return ((long long)x.first)^(((long long)x.second)<<32); } Most competitive programming environments are still 32-bit. So, by doing ^(((long long)x.second)<<32) and then implicitly casting to size_t, you are effectively discarding x.second. Now, the hash depends only on x.first.Here is the code to check that: #include using namespace std; struct HASH{ size_t operator()(const pair&x)const{ return ((long long)x.first)^(((long long)x.second)<<32); } }; unordered_map,int,HASH>m; int main(){ auto T=clock(); for(int i=0;i<20000;i++) m[make_pair (1, i)] = i; cout<
•  » » 4 years ago, # ^ | ← Rev. 5 →   -23 Post edited.
•  » » » 4 years ago, # ^ |   +11 Sorry, I don't get what you are saying. I think you have to work on your math. 858 is smaller than 1000; isn't it? So what? That's still on the order of a second.Note that we insert 20,000 elements, not a million like you do. If inserting twenty thousand, again, elements takes on the order of a second, that's quadratic behavior, or at least definitely not linear. The constant 20,000 is used instead of your code's 1,000,000 so that we can actually see the result in reasonable time. One can make it 1,000,000 and watch it time out (I, for one, wasn't patient enough to let it finish).Also, note that changing make_pair (1, i) to make_pair (i, i) makes the program run in an instant (less than 100 ms). To summarize again: Codeforces checks your solutions in a 32-bit environment. So, size_t is 32 bits long. So, for every possible X and Y, the result of X ^ (((long long)Y)<<32) converted to size_t is just X. So, the hashes for pairs (1, 0), (1, 1), (1, 2), (1, 3), ..., (1, 999999) will be equal, and unordered_map will have trouble storing them.
•  » » » » 4 years ago, # ^ | ← Rev. 2 →   0 I have interested! thank you.I have changed my hash function, you can see the new one.I have mistaken, size_t is alias of unsigned int, no unsigned long long int.
•  » » » » » 4 years ago, # ^ |   0 The new hash approach perhaps does something like x * 37 + y anyway, so why not do that explicitly? struct HASH{ size_t operator()(const pair&x)const{ return (size_t) x.first * 37U + (size_t) x.second; } }; On a side note, I'm surprised that a language as mature as C++ (11) does not define a default hashing method for library containers such as pair or vector.
•  » » » » » » 4 years ago, # ^ | ← Rev. 2 →   +5 It has one for vector: hash >.You can use it for pair, first create a vector of {x.first,x.second} and use hash >.
•  » » » » » » » 4 years ago, # ^ |   0 Can you please elaborate?I try the following: #include using namespace std; unordered_set , hash > () > s; int main() {s.insert ({1, 2, 3});} And it does not compile (MinGW GCC 4.8.1 and 5.1.0), throwing some 100+ lines of error messages at me, basically stating that I misuse hash >. Removing () after hash > does not help.Googling for a few minutes did not help me either. I only found a reference stating that it is not implemented in the library, and a few implementations for custom types.
•  » » » » » » » » 4 years ago, # ^ |   0 You can create it like this: namespace std{ template<>struct hash >{ size_t operator()(vector const& v) const{ unsigned long long h=0; for(auto &x:v) h<<=32,h^=hash()(x),h=hash()(h); return h; } }; } 
•  » » » » » » » » » 4 years ago, # ^ |   0 So, what you are saying is that there is in fact no built-in hash > after all? Then my point still stands.I won't deny that almost anything is possible with C++. I'm just surprised some trivial pieces of that work aren't in the library already.
•  » » 2 years ago, # ^ | ← Rev. 3 →   0 if i want to declare an unordered map like that unordered_map< pair ,int> frq; example : it is something like that : frq[ ( (2,3) , 5 ) ];Or if i want to declare an unordered map like that unordered_map< pair ,pair > frq; example : it is something like that => frq[( (2,3) , (4,5) ) ];How can i write my own hash function ?
 » 4 years ago, # |   0 I run these two versions of map and unordered_map on custome test with GNU G++11 5.1.0. I see that unordered_map is slower than mapVersion of map: #include using namespace std; map,int>m; int main(){ auto T=clock(); for(int i=0;i<1000000;i++) m[make_pair (1, i)] = i; cout< using namespace std; struct HASH{ size_t operator()(const pair&x)const{ return ((long long)x.first)^(((long long)x.second)<<32); } }; unordered_map,int,HASH>m; int main(){ auto T=clock(); for(int i=0;i<1000000;i++) m[make_pair (1, i)] = i; cout<
•  » » 4 years ago, # ^ | ← Rev. 7 →   0 On Intel® Core™ i3 CPU M 390 @ 2.67GHz × 4 :map : 2469 MSunordered_map: 485 MSOn CodeForces:map: 2469 MSunordered_map : 1885 MS
•  » » » 13 months ago, # ^ |   0 hey @Arpa can u explain me the struct HASH code? thanks in advance.....
•  » » » » 13 months ago, # ^ |   0 You should implement operator () for this struct. It takes an object and returns size_t (alias of unsigned int). You should write it in a way such that minimize the number of conflicts.
•  » » 4 years ago, # ^ |   0 Add this two lines: m.reserve(4096); m.max_load_factor(0.25); 
 » 4 years ago, # |   0 Auto comment: topic has been updated by arpa (previous revision, new revision, compare).
 » 4 years ago, # | ← Rev. 3 →   0 What does this mean ? s.max_load_factor(0.25);And which library it uses ?
•  » » 4 years ago, # ^ | ← Rev. 3 →   0 The load factor is the ratio between the number of elements in the container (its size) and the number of buckets (bucket_count). (source)it uses (or ) for sure.
 » 3 years ago, # |   +5 Hi can anyone explain, why map is more than 10 times faster in this case? I could not find any reason for this. any help on this matter would be highy appreciated. I also tried adding the 2 lines of code mentioned above to make unordered_map faster but with no luck. 21740384 and 21740655
•  » » 3 years ago, # ^ |   +5 Because map has a logarithmic access time on size always, on the other hand, unordered_map in average has a constant access time, but in worst case it's just linear in size.
•  » » » 3 years ago, # ^ | ← Rev. 3 →   0 Thanks for the quick reply! I am aware of the worst case complexity of unordered map. However, usually most of the times unordered map is observed to be faster. Can you share any further insights on deciding between unordered map and map. And also what is making unordered map slower than map in the above case I presented? i,e.. what kinds of data is suitable to be represented by map and unordered_map exactly?EDIT: I see that the test case on which unordered map gave TLE involves hashing a multiple of some fixed number. Has this got something to do with the TLE(I believe it must be the reason). Also, was the testcase specifically designed so that unordered_map fails on it, How do we make sure not to fall in such traps if so?
 » 3 years ago, # |   0 Please see the following issue: http://codeforces.com/blog/entry/44705?#comment-292160
•  » » 3 years ago, # ^ |   0 Thanks a lot! I'm sorry for posting on the wrong blog. I should have found that post myself. But I'm new here :D
•  » » 3 years ago, # ^ |   0 This problem can resolved with reserve trick.Like this : 21771219.
•  » » » 3 years ago, # ^ |   +8 It's not a trick if you know how unordered_map works.
•  » » » » 3 years ago, # ^ | ← Rev. 2 →   -8 I know how it works. Read post again carefully.I have explained in post how it works.
 » 3 years ago, # |   0 Hi Arpa,Is there any way to limit the size of the map? I mean for example not to allow for the map to contain more than 256 elements? Is there any built in solution for this. Somewhere I've seen before that it have been used like this: unordered_map map_name(256);Is this valid?Thank you in advance.
 » 2 years ago, # |   +2 Hi. I have 3 questions. Why must the input value for the reserve() method be a power of 2? Is there formula to determine the input value for the reserve() method? Let's say we use mp.reserve(x) where x is a power of 2. Does that mean that x must be >= N, where N is the number of elements that we are going to input into the unordered_map? How did you determine the value for the max_load_factor? Is that supposed to be a magic value? Please advise me.
 » 2 years ago, # |   +18 Thank you for your hard work, but unfortunately I don't find it that much useful with all that painful implementation.
 » 22 months ago, # |   0 AC using map: 33860000TLE using unordered_map: 33860168 [ Yes, I used the tricks you mentioned to make unordered map faster. Also I have used the Hash function that you gave in the post for pairSo, how can I believe that unorded_map is faster than map ??
•  » » 17 months ago, # ^ |   0 It's really confusing when to use unordered_map and when to use map. My submission with map takes 1450 ms: 38906751 Same submission with unordered_map takes 467 ms:38906911 Though both passed time limit (3 s), if the time limit was 1-1.3 s, solution using map would have failed.
•  » » 14 months ago, # ^ |   0 You just need to use a better hash function for pair to get your solution passed. The hash function proposed here for pair clearly isn't a good one. Read the above discussion here. I have changed your hash function and your TLE code gets ac for that problem. Here's the ac code with modified hash function.
•  » » » 14 months ago, # ^ |   0 But is it always safe to use unordered_map in contests, since in worst case unordered_map can be of O(n)? Should anyone use unordered_map always instead of map ?
 » 14 months ago, # |   0 SUMFOUR — 4 values whose sum is 0 on SPOJ in an example where unordered_map with reserve() will give AC and others will give TLE
 » 13 months ago, # |   +3 ََُArpa youre the best :)
 » 10 months ago, # |   0 unordered_map with pair as the key recently gave my time out in my code. I used simple map instead of unordered_map and the code was accepted. The worst case space requirement for the problem was O(n^2) with n <= 1000.My submissions are: map and unordered_map.