zoukankan      html  css  js  c++  java
  • c++ map中key用非简单数据类型时候性能消耗

    typedef pair<long, int> PAIR_X;
    map<long, int> mLongInt;
    map<PAIR_X, int> mPairx;
    
    #define LOOP_MAX    1000000
    
    int main(void)
    {
        time_t tSimpleInsert, tSimpleQuery;
        {
            time_t tBgn = clock();
            for (int i = 0; i < LOOP_MAX; i++)
            {
                mLongInt[i] = i + 100;
            }
            int xCount = 0;
            time_t tInsert = clock();
            tSimpleInsert = tInsert - tBgn;
            for (int i = 0; i < LOOP_MAX; i++)
            {
                auto it = mLongInt.find(i);
                if (it != mLongInt.end())
                {
                    xCount++;
                }
            }
            time_t tQuery = clock();
            tSimpleQuery = tQuery - tInsert;
        }
        
        time_t tPairInsert, tPairQuery;
        {
            time_t tBgn = clock();
            for (int i = 0; i < LOOP_MAX; i++)
            {
                PAIR_X px(i, i + 1);
                mPairx[px] = i + 100;
            }
            int xCount = 0;
            time_t tInsert = clock();
            tPairInsert = tInsert - tBgn;
            for (int i = 0; i < LOOP_MAX; i++)
            {
                PAIR_X px(i, i + 1);
                auto it = mPairx.find(px);
                if (it != mPairx.end())
                {
                    xCount++;
                }
            }
            time_t tQuery = clock();
            tPairQuery = tQuery - tInsert;
        }
    
    
    
        return 0;
    }

    今天突然想到个问题。

    typedef pair<long, int> PAIR_X;
    map<long, int> mLongInt;
    map<PAIR_X, int> mPairx;

    这种写法情况下。mLongInt和mPairx的性能差距大不大。。然后就用vs写了个测试.测试结果还好。100W次insert差个1S多、find差个1s左右。。

    记录下

  • 相关阅读:
    python 读execl文件
    git 命令 个人备忘录
    python-django后端,富文本编辑器防XSS漏洞攻击,过滤富文本XSS
    mi
    Glance docker安装 cinder
    keystore glance
    openstack管理docker管理
    lvm 磁盘 数据库 wordpress 参考答案
    docker
    wordpress
  • 原文地址:https://www.cnblogs.com/yylingyao/p/11648384.html
Copyright © 2011-2022 走看看