unordered_map,unordered_set,map,set 插入查找耗时测试

对于10^7的随机固定数列在g++4.8.1插入查找测试

通过下表使用 unorder_map时不要用insert插入

【插入】
unordered_set::insert(int) 259
unordered_map[int] = int 296
unordered_map .insert() 3000
map[int] = int 1601
set::insert(int) 1703
【查找】
unordered_set::find(int) 108
unordered_set::count(int) 156
unordered_map::find(int) 112
int=unordered_map[int] 134
unordered_map::count(int) 162
map::find(int) 1414
int = map[int] 1426
set:find(int) 1425

【测试程序】

#include <time.h>
#include <cstdio>
#include <cstdlib>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
#define MAXN 10000000
#define MOD 1000000000
unordered_set<int> uset;
unordered_map<int, int> umap;
map<int, int> mp;
set<int> stt;
int in[MAXN];

int main()
{
	srand(0);
	long long sum = 0;
	int t = 10;
	uset.clear();
	umap.clear();
	mp.clear();
	stt.clear();
	for (int i = 0; i < MAXN; i++)
	{
		in[i] = rand() % MOD;
		//uset.insert(in[i]);
		//umap[in[i]] = i;
		//mp[in[i]] = i;
		stt.insert(in[i]);
	}
	while (t--)
	{
		long long st = clock();
		for (int i = 0; i < MAXN; i++)
		{
			//const auto it = uset.find(in[i]);
			//const auto it = uset.count(in[i]);

			//const auto it = umap.find(in[i]);
			//const auto it = umap[in[i]];
			//const auto it = umap.count(in[i]);

			//const auto it= mp.find(in[i]);
			//const auto it= mp[in[i]];

			const auto it= stt.find(in[i]);
		}
		long long ed = clock();
		sum += ed - st;
	}
	printf("%lld\n", sum / 10);
	return 0;
}

 

发表评论

邮箱地址不会被公开。 必填项已用*标注

请输入正确的验证码