hash_map不是標準庫,因此不同平臺下包含頭文件不同, 前綴也不同,這里使用了一個通用定義
1: #ifdef _WIN32
2: #include <hash_map>
3: #define HASHMAP_PREFIX stdext
4: #else
5: #include <ext/hash_map>
6: #define HASHMAP_PREFIX __gnu_cxx
7: #endif
對于初始桶大小設置,Linux下使用hash_map構造函數可以設置, Windows下則沒有對應的設置函數.
查閱Windows下hash_map的源碼,并在hash_map()默認構造函數旁邊添加一個測試用初始桶設置函數
1: hash_map( size_type _Buckets )
2: : _Mybase(key_compare(), allocator_type())
接下來使用相同的測試代碼
1: const uint32 Buckets = 1000;
2: HASHMAP_PREFIX::hash_map<uint32,uint32> MyHash( Buckets );
3:
4: TimeRuler Ruler;
5: for ( uint32 i = 0; i <1000000;i++)
6: {
7: MyHash[i] = i;
8: }
9:
10: printf("%d\n", Ruler.GetCostTime() );
這里的TimeRuler是使用boost timer的時間戳封裝
Release下測試結果:
OS \ Buckets |
8 ( default ) |
1000 |
Win7 |
430ms |
560ms |
Mint( VMware ) |
127ms |
127ms |
Windows的測試結果說明, 不給出桶初始化函數是正確的, 默認管理比自己設置更高效.
Linux平臺感覺很詭異, 不清楚是不是虛擬機造成的結果不準確