案例分享c++ map的使用和 查找性能测试


本文摘自网络,作者php是最好的语言,侵删。

最近在为推荐服务作性能调优,这个服务的主要逻辑是用离线计算的模型数据给请求中的每个广告打分,再返回这些广告的排序结果,这里面打分的过程其实就用请求中的数据拼成各种key,去查一个大的 map,这种计算非常多,成为了主要的性能瓶颈,代码比较老,使用的是 boost::unordered_map,为了解决这个问题,找了一些第三方库和标准库对比了一下

下面是在一台 aws r4.xlarge 机器上的测试结果(注意编译的时候一定要加 -O2):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

std::map<int, int>                                 => 51866903

std::unordered_map<int, int>                       => 3838175

std::unordered_map<int, int, nohashint>            => 3508570

std::unordered_map<int, int>(N)                    => 3804471

boost::unordered_map<int, int>                     => 3291384

boost::unordered_map<int, int, nohashint>          => 3293934

boost::unordered_map<int, int>(N)                  => 3265856

google::dense_hash_map<int, int>                   => 785969

google::dense_hash_map<int, int, nohashint>        => 784455

google::dense_hash_map<int, int>(N)                => 899262

tsl::hopscotch_map<int, int>                       => 654668

tsl::hopscotch_map<int, int, nohashint>            => 680964

tsl::hopscotch_map<int, int>(N)                    => 663607

tsl::robin_map<int, int>                           => 406176

tsl::robin_map<int, int, nohashint>                => 411358

tsl::robin_map<int, int>(N)                        => 409993

可以看到 tsl::robin_map 的性能基本上能达到 std::unordered_map 的 10 倍,这个性能和操作系统以及库版本也有一定关系,实际生产环境中建议把代码拉下来在自己的环境下测试一下

阅读剩余部分

相关阅读 >>

dev c++怎么用

c++中判断重载方法的依据是什么

c++中头文件和源文件的区别是什么

c++类型转换

c语言 三种求回文数的算法

基于汇编的 c/c++ 协程(用于服务器)的实现

c++输出二维字符矩阵对齐

c++如何获取数值极值的办法

技术解答面向对象的初步认识(c++ 类)

【c++】深入了解继承方式基础知识及其与访问限定符的关系

更多相关阅读请进入《性能》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    正在狠努力加载,请稍候...