hash_map不是標(biāo)準(zhǔn)庫,因此不同平臺(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
對于初始桶大小設(shè)置,Linux下使用hash_map構(gòu)造函數(shù)可以設(shè)置, Windows下則沒有對應(yīng)的設(shè)置函數(shù).
查閱Windows下hash_map的源碼,并在hash_map()默認(rèn)構(gòu)造函數(shù)旁邊添加一個(gè)測試用初始桶設(shè)置函數(shù)
1: hash_map( size_type _Buckets )
2: : _Mybase(key_compare(), allocator_type())
3: {
4: _Init( _Buckets );
5: }
接下來使用相同的測試代碼
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下測試結(jié)果:
OS \ Buckets | 8 ( default ) | 1000 |
Win7 | 430ms | 560ms |
Mint( VMware ) | 127ms | 127ms |
Windows的測試結(jié)果說明, 不給出桶初始化函數(shù)是正確的, 默認(rèn)管理比自己設(shè)置更高效.
Linux平臺(tái)感覺很詭異, 不清楚是不是虛擬機(jī)造成的結(jié)果不準(zhǔn)確
posted on 2012-02-23 18:54 戰(zhàn)魂小筑 閱讀(1585) 評論(0) 編輯 收藏 引用 所屬分類: 網(wǎng)絡(luò) 服務(wù)器技術(shù) 、C++/ 編程語言