map unordered_map hash_map的查找性能测试

结论如下:

Release模式下:

1. 容量为10的时候,查找效率:map > unordered_map > hash_map

2. 容量为100的时候,查找效率:map = unordered_map > hash_map

3. 容量为1000的时候,查找效率:unordered_map > hash_map > 4倍map

4. 容量为1万的时候,查找效率:hash_map > unordered_map > 4倍map

5. 容量为10万的时候,查找效率:hash_map > unordered_map > 4倍map

6. 容量为100万的时候,查找效率:hash_map > unordered_map > 1.6倍map

7. 容量为1000万的时候,查找效率:hash_map > unordered_map > 1.4倍map

8. 容量为1亿的时候,程序崩溃了,哈哈哈哈哈哈,如果你知道原因请告诉我

9. debug模式下和release模式下,差距非常大,几百倍的差距。。。。。

Debug模式下:

1. 查找效率:hash_map > unordered_map > map

2. 随着容量的增加,hash_map, unordered_map的查找效率有所降低,但浮动不大毕竟是常量级别。map的效率直线下降。。。

3. 容量为一千万的时候,程序同样崩溃

实验结果如下图:

Release模式                                                                                Debug模式(注意:相比Release模式还降低了10倍的查询量)

    

 

代码如下:

#include<iostream>
#include<map>
#include<hash_map>
#include<unordered_map>
#include<boost/progress.hpp>

using namespace std;
using namespace boost;

// 测试函数,
// size        :    map的实际大小
// times    :    查找的轮次,每一轮次都从0查找到size-1
void test(int size, int times)
{
    cout << "size=" << size << "; times=" << times << endl;

    map<int, int>                m;
    unordered_map<int, int>        um;
    hash_map<int, int>            hm;

    // 初始化
    for (int i = 0; i<size; i++)
    {
        m[i] = i;
        um[i] = i;
        hm[i] = i;
    }

    // map的查找
    {
        int count = 0;
        progress_timer t; // progress_timer变量会在创建时计时,析构时自动打印出耗时,所以不会受到前面初始化的影响,下同,不再解释
        for (int i = 0; i<times; i++)
        {
            for (int j = 0; j<size; j++)
            {
                if (m.find(j) != m.end())
                {
                    count++;
                }
            }
        }
        cout << "count:" << count <<", map:";
    }


    // unordered_map的查找
    {
        int count = 0;
        progress_timer t;
        for (int i = 0; i<times; i++)
        {
            for (int j = 0; j<size; j++)
            {
                if (um.find(j) != um.end())
                {
                    count++;
                }
            }
        }
        cout << "count:" << count << ", unordered_map:";
    }

    // hash_map的查找
    {
        int count = 0;
        progress_timer t;
        for (int i = 0; i<times; i++)
        {
            for (int j = 0; j<size; j++)
            {
                if (hm.find(j) != hm.end())
                {
                    count++;
                }
            }
        }
        cout << "count:" << count << ", hash_map:";
    }
}

int main()
{
    test(10,10000000);        // 容量:10        查找:1千万轮次

    test(100, 1000000);        // 容量:100        查找:1百万轮次

    test(1000, 100000);        // 容量:1000        查找:10万轮次

    test(10000, 10000);        // 容量:10000    查找:1万轮次

    test(100000, 1000);        // 容量:100000    查找:1000轮次

    test(1000000, 100);        // 容量:1000000    查找:100轮次

    test(10000000, 10);        // 容量:10000000    查找:10轮次

    getchar();
    return 0;
}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值