#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#