hash_map不是標(biāo)準(zhǔn)庫(kù),因此不同平臺(tái)下包含頭文件不同, 前綴也不同,這里使用了一個(gè)通用定義
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
對(duì)于初始桶大小設(shè)置,Linux下使用hash_map構(gòu)造函數(shù)可以設(shè)置, Windows下則沒(méi)有對(duì)應(yīng)的設(shè)置函數(shù).
查閱Windows下hash_map的源碼,并在hash_map()默認(rèn)構(gòu)造函數(shù)旁邊添加一個(gè)測(cè)試用初始桶設(shè)置函數(shù)
1: hash_map( size_type _Buckets )
2: : _Mybase(key_compare(), allocator_type())
接下來(lái)使用相同的測(cè)試代碼
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的時(shí)間戳封裝
Release下測(cè)試結(jié)果:
OS \ Buckets |
8 ( default ) |
1000 |
Win7 |
430ms |
560ms |
Mint( VMware ) |
127ms |
127ms |
Windows的測(cè)試結(jié)果說(shuō)明, 不給出桶初始化函數(shù)是正確的, 默認(rèn)管理比自己設(shè)置更高效.
Linux平臺(tái)感覺(jué)很詭異, 不清楚是不是虛擬機(jī)造成的結(jié)果不準(zhǔn)確