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]$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值