hash_map构造函数设与不设初始bucket数的区别
来源:互联网 发布:淘宝办理幽灵户口 编辑:程序博客网 时间:2024/04/29 22:17
根据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]$
- hash_map构造函数设与不设初始bucket数的区别
- css+div设计时IE与Firefox的主要区别
- WinForm中设计时与DesignMode的区别
- css+div设计时IE与Firefox的主要区别
- 输出端口设为reg与wire的区别
- 概设与祥设
- 初始建库SGA该设多大、PGA该设多大的建议
- 初始建库SGA该设多大 PGA该设多大的建议
- CrystaleDcisions.ReportSource.ReportSourceFatory的类型初始设定项引发异常
- 人生不设回收站
- 如何设等概率的随机函数
- Spring之构造注入与设值注入
- 为什么不能把构造函数设为虚函数,却可以把析构函数设为虚函数
- Hash_Map 与 Map的区别
- 文章管理系统的系统分析与设
- 加入收藏与设为首页的设置。
- 设值注入和构造注入的比较
- 设值注入和构造注入的比较
- SVN 安装和使用
- POJ_2234_博弈论
- 维吉尼亚(Vigenère)密码的加密和解密以及方阵(js版)
- 尼姆博奕(Nimm Game)
- Win7/R2 原生VHD 启动 详解
- hash_map构造函数设与不设初始bucket数的区别
- log4j的使用
- 网页实现滚动
- [读书摘记]调用另一个Activity--Intent对像的使用
- 在 Visio 中创建折线图
- Linux内核模块编程简介
- CString格式化字符串
- 怎么查看自己下载过哪些资源?
- mysql+php无法连接sql(已解决)