`

hash_map构造函数设与不设初始bucket数的区别

阅读更多


根据hash_map的实现进行理论猜测,不设初始bucket数时,插入是会比较慢的。
实际测试证实如果设置合适的初始bucket数效率较高,快1倍左右。

============================================

不设初始bucket数时

#include <map>
#include <ext/hash_map>
#include <time.h>

using namespace __gnu_cxx;
using namespace std;

const unsigned item_cnt = 1000000;

int main()
{
clock_t start, finish;
start = clock();

hash_map<unsigned, unsigned> hashMap; //不设bucket数,构造空hash_map
unsigned i;
for(i = 0; i < item_cnt; i++)
hashMap[i] = i;

finish = clock();
printf( "cnst and insert time: %f seconds\n", (double)(finish - start) / CLOCKS_PER_SEC);

start = clock();
for(i = 0; i < item_cnt; i++)
if(hashMap.find(i) == hashMap.end())
printf("error: item not found\n.");
finish = clock();

printf("size: %u\n", hashMap.size());
printf("max_size: %u\n", hashMap.max_size());
printf("bucket_size: %u\n", hashMap.bucket_count());

printf( "search time: %f seconds\n", (double)(finish - start) / CLOCKS_PER_SEC);

return 0;
}

[ljl@tcdzq cplus]$ g++ -o hash.out hash_map.cpp
[ljl@tcdzq cplus]$ ./hash.out
cnst and insert time: 0.000000 seconds
size: 10000
max_size: 4294967295
bucket_size: 12289
search time: 0.000000 seconds
[ljl@tcdzq cplus]$ ./hash.out
cnst and insert time: 0.000000 seconds
size: 10000
max_size: 4294967295
bucket_size: 12289
search time: 0.010000 seconds
[ljl@tcdzq cplus]$ g++ -o hash.out hash_map.cpp
[ljl@tcdzq cplus]$ ./hash.out
cnst and insert time: 0.100000 seconds
size: 100000
max_size: 4294967295
bucket_size: 196613
search time: 0.020000 seconds
[ljl@tcdzq cplus]$ ./hash.out
cnst and insert time: 0.100000 seconds
size: 100000
max_size: 4294967295
bucket_size: 196613
search time: 0.020000 seconds
[ljl@tcdzq cplus]$ g++ -o hash.out hash_map.cpp
[ljl@tcdzq cplus]$
[ljl@tcdzq cplus]$
[ljl@tcdzq cplus]$
[ljl@tcdzq cplus]$ ./hash.out
cnst and insert time: 0.930000 seconds
size: 1000000
max_size: 4294967295
bucket_size: 1572869
search time: 0.220000 seconds
[ljl@tcdzq cplus]$ ./hash.out
cnst and insert time: 0.930000 seconds
size: 1000000
max_size: 4294967295
bucket_size: 1572869
search time: 0.220000 seconds
[ljl@tcdzq cplus]$
[ljl@tcdzq cplus]$
[ljl@tcdzq cplus]$
[ljl@tcdzq cplus]$

=================================

设初始bucket数时


#include <map>
#include <ext/hash_map>
#include <time.h>

using namespace __gnu_cxx;
using namespace std;

const unsigned item_cnt = 1000000;

int main()
{
clock_t start, finish;
start = clock();

hash_map<unsigned, unsigned> hashMap(item_cnt);
//hash_map<unsigned, unsigned> hashMap((unsigned)(item_cnt * 1.5)); //与上行的测试结果一样
unsigned i;
for(i = 0; i < item_cnt; i++)
hashMap[i] = i;

finish = clock();
printf( "cnst and insert time: %f seconds\n", (double)(finish - start) / CLOCKS_PER_SEC);

start = clock();
for(i = 0; i < item_cnt; i++)
if(hashMap.find(i) == hashMap.end())
printf("error: item not found\n.");
finish = clock();

printf("size: %u\n", hashMap.size());
printf("max_size: %u\n", hashMap.max_size());
printf("bucket_size: %u\n", hashMap.bucket_count());

printf( "search time: %f seconds\n", (double)(finish - start) / CLOCKS_PER_SEC);

return 0;
}
[ljl@tcdzq cplus]$
[ljl@tcdzq cplus]$
[ljl@tcdzq cplus]$
[ljl@tcdzq cplus]$ g++ -o hash.out hash_map.cpp
[ljl@tcdzq cplus]$ ./hash.out
cnst and insert time: 0.470000 seconds
size: 1000000
max_size: 4294967295
bucket_size: 1572869
search time: 0.210000 seconds
[ljl@tcdzq cplus]$ ./hash.out
cnst and insert time: 0.460000 seconds
size: 1000000
max_size: 4294967295
bucket_size: 1572869
search time: 0.220000 seconds
[ljl@tcdzq cplus]$
[ljl@tcdzq cplus]$
[ljl@tcdzq cplus]$ g++ -o hash.out hash_map.cpp
[ljl@tcdzq cplus]$ ./hash.out
cnst and insert time: 0.040000 seconds
size: 100000
max_size: 4294967295
bucket_size: 196613
search time: 0.020000 seconds
[ljl@tcdzq cplus]$ ./hash.out
cnst and insert time: 0.040000 seconds
size: 100000
max_size: 4294967295
bucket_size: 196613
search time: 0.020000 seconds
[ljl@tcdzq cplus]$
[ljl@tcdzq cplus]$ g++ -o hash.out hash_map.cpp
[ljl@tcdzq cplus]$ ./hash.out
cnst and insert time: 0.000000 seconds
size: 10000
max_size: 4294967295
bucket_size: 12289
search time: 0.000000 seconds
[ljl@tcdzq cplus]$ ./hash.out
cnst and insert time: 0.000000 seconds
size: 10000
max_size: 4294967295
bucket_size: 12289
search time: 0.000000 seconds
[ljl@tcdzq cplus]$

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics