关于map,hash_map小数据量查询效率的问题

关于map>

Multimap

Hash_map>

Hash_multimap

对于数据量不大情况下的测试情况:

查询key值为4的情况下循环100万次得出的查询时间:

Hash_map执行时间为868.575351秒

Hash_multimap 执行时间892.441939秒

Map_vector执行时间:717.130047秒

Multimap执行时间为749.253617秒

// algorithm.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include 
#include
#include
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{


	vector coll = { 2, 5, 4, 1, 6, 3 };

	auto minpos = min_element(coll.begin(), coll.end());
	cout << "min : " << *minpos << endl;
	auto maxpos = max_element(coll.begin(), coll.end());
	cout << "max : " << *maxpos << endl;

	sort(coll.begin(),coll.end());

	for (auto elem : coll)
	{
		cout << elem << " ";
	}
	cout << endl;
	auto pos3 = find(coll.begin(),coll.end(),3);
	auto pos6 = find(coll.begin(),coll.end(),6);
	reverse(pos3,pos6);

	for (auto elem : coll)
	{
		cout << elem << " ";
	}
	cout << endl;

	/*处理多重区间(multipleranges)*/
	list coll1 = {1,2,3,4,5,6,7,8,9};
	vector coll2;
	//resize 的作用是改变coll2的元素个数
	coll2.resize(coll1.size());
	copy(coll1.cbegin(),coll1.cend(),coll2.begin());

	deque coll3(coll1.size());

	copy(coll1.begin(),coll1.end(),coll3.begin());
	/*hash_map*/
#if 0
	hash_multimap t_hash_map;
	
	t_hash_map.insert(make_pair(1,"aa"));
	t_hash_map.insert(make_pair(1,"bb"));
	t_hash_map.insert(make_pair(1,"cc"));
	t_hash_map.insert(make_pair(2, "22"));
	t_hash_map.insert(make_pair(3, "33"));
	
	t_hash_map.insert(make_pair(7, "77"));

	t_hash_map.insert(make_pair(4, "44"));
	t_hash_map.insert(make_pair(4, "444"));
	t_hash_map.insert(make_pair(4, "4444"));
	t_hash_map.insert(make_pair(5, "55"));
	t_hash_map.insert(make_pair(6, "66"));
	t_hash_map.insert(make_pair(4, "44444"));
	t_hash_map.insert(make_pair(5, "555"));
	t_hash_map.insert(make_pair(6, "666"));
	t_hash_map.insert(make_pair(4, "444444"));
	t_hash_map.insert(make_pair(4, "4444444"));
	
	t_hash_map.insert(make_pair(7, "77777"));

	multimap multimap_map;

	multimap_map.insert(make_pair(1, "aa"));
	multimap_map.insert(make_pair(1, "bb"));
	multimap_map.insert(make_pair(1, "cc"));
	multimap_map.insert(make_pair(2, "22"));
	multimap_map.insert(make_pair(3, "33"));

	multimap_map.insert(make_pair(7, "77"));

	multimap_map.insert(make_pair(4, "44"));
	multimap_map.insert(make_pair(4, "444"));
	multimap_map.insert(make_pair(4, "4444"));
	multimap_map.insert(make_pair(5, "55"));
	multimap_map.insert(make_pair(6, "66"));
	multimap_map.insert(make_pair(4, "44444"));
	multimap_map.insert(make_pair(5, "555"));
	multimap_map.insert(make_pair(6, "666"));
	multimap_map.insert(make_pair(4, "444444"));
	multimap_map.insert(make_pair(4, "4444444"));

	multimap_map.insert(make_pair(7, "77777"));


	hash_map> map_vec;
	map> m_map_vec;
	vector t_vec;
	t_vec.push_back("aa");
	t_vec.push_back("bb");
	t_vec.push_back("cc");
	map_vec.insert(make_pair(1,t_vec));
	m_map_vec.insert(make_pair(1, t_vec));
	t_vec.clear();

	t_vec.push_back("22");
	map_vec.insert(make_pair(2,t_vec));
	m_map_vec.insert(make_pair(2, t_vec));
	t_vec.clear();

	t_vec.push_back("33");
	map_vec.insert(make_pair(3,t_vec));
	m_map_vec.insert(make_pair(3, t_vec));
	t_vec.clear();

	t_vec.push_back("55");
	t_vec.push_back("555");
	map_vec.insert(make_pair(5, t_vec));
	m_map_vec.insert(make_pair(5, t_vec));
	t_vec.clear();

	t_vec.push_back("66");
	t_vec.push_back("666");
	map_vec.insert(make_pair(6, t_vec));
	m_map_vec.insert(make_pair(6, t_vec));

	t_vec.clear();

	t_vec.push_back("44");
	t_vec.push_back("444");
	t_vec.push_back("4444");
	t_vec.push_back("44444");
	t_vec.push_back("444444");
	t_vec.push_back("4444444");
	map_vec.insert(make_pair(4,t_vec));
	m_map_vec.insert(make_pair(4, t_vec));
	t_vec.clear();

	

	t_vec.push_back("77");
	t_vec.push_back("77777");
	map_vec.insert(make_pair(7, t_vec));
	m_map_vec.insert(make_pair(7, t_vec));
	t_vec.clear();

	LARGE_INTEGER f;
	QueryPerformanceFrequency(&f);//获取内部高精度计数器的频率  

	double dFreq;
	dFreq = (double)f.QuadPart; //获取计数器的频率  

	LARGE_INTEGER time_start, time_end;
	QueryPerformanceCounter(&time_start);//获取内部高精度计数器当前的计数值  
	for (auto i = 1; i < 1000000; i++)
	{
#if 0
#if 0
		auto start = t_hash_map.equal_range(4).first;
		auto end = t_hash_map.equal_range(4).second;
		for (auto hash_iterator = start; hash_iterator != end; hash_iterator++)
		{
			cout << hash_iterator->second << endl;
		}
#else
		auto start = m_map_vec.equal_range(4).first;
		auto end = m_map_vec.equal_range(4).second;
		for (auto map_iterator = start; map_iterator != end; map_iterator++)
		for (auto vec_iterator = map_iterator->second.begin(); vec_iterator != map_iterator->second.end();vec_iterator++)
		{
			cout << *vec_iterator << endl;
		}
#endif
#else
#if 0
		auto start = map_vec.equal_range(4).first;
		auto end = map_vec.equal_range(4).second;
		for (auto hash_iterator = start; hash_iterator != end; hash_iterator++)
		for (auto vec_iterator = hash_iterator->second.begin(); vec_iterator != hash_iterator->second.end();vec_iterator++)
		{
			cout << *vec_iterator << endl;
		}
#else
		auto start = multimap_map.equal_range(4).first;
		auto end = multimap_map.equal_range(4).second;
		for (auto multimap_iterator = start; multimap_iterator != end; multimap_iterator++)
		{
			cout << multimap_iterator->second << endl;
		}
#endif
#endif
	}
	QueryPerformanceCounter(&time_end);

	//时间差 = 计数值差/频率(单位s)  
	double duration = (double)(time_end.QuadPart - time_start.QuadPart) / dFreq;
	printf("%f\n", duration);// 3.969499  
#endif
	/*re*/
	getchar();
	return 0;
}

empty

你可能感兴趣的:(C/C++)