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;
}
发布了105 篇原创文章 · 获赞 58 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/yzf279533105/article/details/102811230