std::map结构压测

文章通过一个名为`measureTimer`的类来测量`std::map`操作的时间消耗,包括插入(int和string类型键值对)、查找和计算元素数量。测试结果显示,int类型的操作比string类型更快,且单次查找的时间消耗远低于多次循环查找。
摘要由CSDN通过智能技术生成
#include <iostream>
#include <sstream>
#include <ctime>
#include <map>
#include <string>
#include <chrono>
#include <thread>

class measureTimer
{
public:
    measureTimer(){   update();  }

    void update() {  last = HrClock::now();  }

    double diff(const char *descriptionStr = nullptr) { /* return milliseconds escaped */
        now = HrClock::now();
        double result = std::chrono::duration_cast<MicroSeconds>(now - last).count() / 1e3;
        if (descriptionStr)  std::cout << "diff of " << descriptionStr << " result=" << result << "ms" << std::endl;
        return result;
    }

    /* time delay with C++11 - #include <thread> */
    #define jn_sleep(for_seconds)   std::this_thread::sleep_for(std::chrono::seconds(for_seconds))
    #define jn_msleep(for_milliseconds)        std::this_thread::sleep_for(std::chrono::milliseconds(for_milliseconds))

private:
    using HrClock = std::chrono::high_resolution_clock;
    using MicroSeconds = std::chrono::microseconds;

    HrClock::time_point last{MicroSeconds::zero()};
    HrClock::time_point now{MicroSeconds::zero()};
};

#define MAX_NUM 1000000

int main()
{
	int nRet = 0;
	measureTimer measureTime; // timer
	
	std::cout << "max size:" << MAX_NUM << std::endl << std::endl;


	std::cout << "/***************** === 键值为 int 类型的 map === ******************/" << std::endl;
	std::map<int, int> mapInt;     //键值为int类型的map

	measureTime.update();
	for(int i=0; i<MAX_NUM; i++)
	{
		//mapInt.insert(make_pair(i, i));
		//mapInt.emplace(std::make_pair(i, i));
		//mapInt.emplace(i, i);
		mapInt[i] = i; //区别为遇到重复键时,insert不会覆盖值,[]会覆盖值
	}

	std::cout << "int-insert  耗时" << measureTime.diff() << "毫秒" << std::endl;
	
	std::map<int, int>::iterator iterInt;

	measureTime.update();
	for(int i=0; i<MAX_NUM; i++)
	{
		iterInt = mapInt.find((MAX_NUM/2));
	}


	std::cout << "int-find  耗时" << measureTime.diff() << "毫秒" << std::endl;
	measureTime.update();

	for(int i=0; i<MAX_NUM; i++)
	{
		nRet = mapInt.count((MAX_NUM/2));
	}
	std::cout << "int-count 耗时" << measureTime.diff() << "毫秒" << std::endl;
	measureTime.update();
	
        const auto &iterIntFind = mapInt.find(8000);
        if ( iterIntFind != mapInt.end() ) {
        	std::cout << "int-single-find 耗时" << measureTime.diff() << "毫秒" << std::endl;
        }



	std::cout << std::endl << "/***************** === 键值为string类型的map === ******************/" << std::endl;
	std::map<std::string, std::string> mapString;
	std::stringstream ss;
	for(int i=0; i<MAX_NUM; i++)
	{
		ss.str("");
		ss << i; 
		//mapString.insert(make_pair(ss.str(), ss.str()));
        //mapString.emplace(std::make_pair(ss.str(), ss.str()));
        mapString.emplace(ss.str(), ss.str());
		//mapString[ss.str()] = ss.str();//或mapString.insert(make_pair(str, str));
	}

	std::cout << "string-insert  耗时" << measureTime.diff() << "毫秒" << std::endl;
	measureTime.update();

	std::map<std::string, std::string>::iterator iterStr;
	for(int i=0; i<MAX_NUM; i++)
	{
		iterStr = mapString.find("25000");
	}

	std::cout << "string-find  耗时" << measureTime.diff() << "毫秒" << std::endl;
	measureTime.update();

	for(int i=0; i<MAX_NUM; i++)
	{
		nRet = mapString.count("25000");
	}
	std::cout << "string-count 耗时" << measureTime.diff() << "毫秒" << std::endl;

	measureTime.update();
	const auto &iter = mapString.find("8000");
	if (iter != mapString.end()) {
		//std::cout << "found it:" << iter->second << std::endl;
		std::cout << "string-single-find 耗时" << measureTime.diff() << "毫秒" << std::endl;
	}

	return 0;
}

out:

-bash-4.2# ./a.out
max size:1000000

/***************** === 键值为 int 类型的 map === ******************/
int-insert  耗时1206.92毫秒
int-find  耗时513.395毫秒
int-count 耗时508.041毫秒
int-single-find 耗时0.007毫秒

/***************** === 键值为string类型的map === ******************/
string-insert  耗时1829.96毫秒
string-find  耗时777.49毫秒
string-count 耗时793.431毫秒
string-single-find 耗时0.012毫秒
-bash-4.2#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值