unorderMap基准性能

插入和删除基本都是0.1us

测试10w条数据

//
//
// unordered_map::emplace
#include <iostream>
#include <string>
#include <unordered_map>
#include "sys/time.h"
#include <unistd.h>
#define MIN(A, B)        ((B) < (A) ? (B) : (A))
#define MAX(A, B)        ((B) > (A) ? (B) : (A))

typedef unsigned long long uint64;

uint64 us_clock_now(){
    struct timeval st;
    gettimeofday(&st, NULL);
    return st.tv_sec * 1000 * 1000 + st.tv_usec;
}

#define max_num 100000
class Position{
};

int main ()
{
    std::unordered_map<void*, int> mymap;
    Position *test[max_num];
    for (int i=0;i<max_num;i++){
        Position* now = new Position();
        test[i] = now;
    }
    uint64 begin = us_clock_now();
    for(int i=0;i<max_num;i++){
        mymap.emplace(test[i], i);
//        mymap.insert(pair<void*,int>(test[i], i));
    }
    uint64 end = us_clock_now();
    for(int i=0;i<max_num;i++){
        if (mymap.find(test[i]) == mymap.end()){
            printf("error\n");
        }
        mymap.erase(test[i]);
    }
    uint64 end2 = us_clock_now();

    printf("insert_time=%llu, erase_time=%llu\n", end-begin, end2-end);
    std::cout << std::endl;
    return 0;
}

  

posted @ 2023-06-22 11:41  stupidstan2019  阅读(28)  评论(0编辑  收藏  举报