c++性能之map实现性能比较
2024-10-20 09:20:40
http://www.cnblogs.com/zhjh256/p/6346501.html讲述了基本的map操作,在测试的时候,发现map的性能极为低下,与java相比相差了接近200倍。测试的逻辑如下:
// map定义
map<int, FirstCPPCls*> mapStudent;
for (i=;i<;i++) {
FirstCPPCls clz;
clz.setAppVersion("12.32.33");
clz.setClusterName("osm-service");
clz.setCompanyId("");
clz.setServiceId("sysL.1.223");
clz.setSubSystemId("");
clz.setSystemId("");
mapStudent.insert(pair<int, FirstCPPCls*>(i, &clz));
} // 获取时间相对计数器, vc专用
begin = GetTickCount();
for (j=;j<;j++) {
for (f=;f<;f++) {
// map查找
mapStudent.find(f);
}
}
end = GetTickCount(); // 打印时间差
cout << "Map查找耗时:" << (end - begin) << endl; // 平均4秒左右
system("pause");
在java中相同的实现,get 100 0000次只花费了20ms。于是搜索 c++ map性能,看了两个帖子如下:
http://blog.csdn.net/a418382926/article/details/22302907
http://blog.sina.com.cn/s/blog_5f93da790101hxxi.html
http://www.ideawu.net/blog/archives/751.html
随后,进行hash_map和unordered_map测试,如下:
// hash_map定义
hash_map<int, FirstCPPCls*> hash_mapStudent;
for (i=;i<;i++) {
FirstCPPCls clz;
clz.setAppVersion("12.32.33");
clz.setClusterName("osm-service");
clz.setCompanyId("");
clz.setServiceId("sysL.1.223");
clz.setSubSystemId("");
clz.setSystemId("");
hash_mapStudent.insert(pair<int, FirstCPPCls*>(i, &clz));
} // 获取时间相对计数器, vc专用
begin = GetTickCount();
for (j=;j<;j++) {
for (f=;f<;f++) {
// map查找
hash_mapStudent.find(f);
}
}
end = GetTickCount(); // 打印时间差
cout << "HashMap查找耗时:" << (end - begin) << endl; // 平均4秒左右
system("pause"); // hash_map定义
unordered_map<int, FirstCPPCls*> unordered_mapStudent;
for (i=;i<;i++) {
FirstCPPCls clz;
clz.setAppVersion("12.32.33");
clz.setClusterName("osm-service");
clz.setCompanyId("");
clz.setServiceId("sysL.1.223");
clz.setSubSystemId("");
clz.setSystemId("");
unordered_mapStudent.insert(pair<int, FirstCPPCls*>(i, &clz));
} // 获取时间相对计数器, vc专用
begin = GetTickCount();
for (j=;j<;j++) {
for (f=;f<;f++) {
// map查找
unordered_mapStudent.find(f);
}
}
end = GetTickCount(); // 打印时间差
cout << "UnorderedMap查找耗时:" << (end - begin) << endl; // 平均4秒左右
system("pause");
输出如下:
HashMap查找耗时:1610
请按任意键继续. . .
UnorderedMap查找耗时:1797
请按任意键继续. . .
虽然,相比std::map,确实提升了50%多,但是跟java,还是慢的一塌糊涂,因为对stl还没有研究,不确定具体什么原因导致。
最新文章
- python 生成器等语法
- [hdu 4416]Good Article Good sentence
- Python字典笔记
- 【AngularJs】---JSONP跨域访问数据传输
- Ubuntu环境下手动配置tomcat
- java_method_readFile读取文件文本txt
- 将日志写入EventLog
- IDisposable接口详解
- java 解析 xml (DOM方法全)
- SE 2014 年4月21日(二)
- 浏览器缓存机制<;转>;
- MARKY一下。
- Caffe 深度学习框架上手教程
- 25个c#知识点
- 使用Fiddler重定向App的网络请求
- 博客和Github简单练习
- AngularJS - Directive Restrictions
- 微信小程序 微信支付
- python struct详解
- COALESCE操作符
热门文章
- POJ--3321 Apple Tree(树状数组+dfs(序列))
- 老师的blog整理
- 计蒜客 30999 - Sum - [找规律+线性筛][2018ICPC南京网络预赛J题]
- codeforces 894B - Ralph And His Magic Field - [数学题]
- CCCC L2-007. 家庭房产 建图 XJB模拟
- RHEL6 Systemtap 安装笔记
- java.io.File实战
- oracle(六) physical read and logical read
- qt——QT中QWidget、QDialog及QMainWindow的区别
- Sql Server 2016 Always On集群搭建