Click here to Skip to main content
15,915,750 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionimplementation of a key value array (not using std::map) ? Pin
doug252-Dec-09 11:41
doug252-Dec-09 11:41 
QuestionRe: implementation of a key value array (not using std::map) ? Pin
CPallini2-Dec-09 12:02
mveCPallini2-Dec-09 12:02 
AnswerRe: implementation of a key value array (not using std::map) ? Pin
doug252-Dec-09 12:17
doug252-Dec-09 12:17 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
«_Superman_»2-Dec-09 12:27
professional«_Superman_»2-Dec-09 12:27 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
Richard MacCutchan2-Dec-09 12:38
mveRichard MacCutchan2-Dec-09 12:38 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug252-Dec-09 12:46
doug252-Dec-09 12:46 
AnswerRe: implementation of a key value array (not using std::map) ? Pin
Stuart Dootson2-Dec-09 22:57
professionalStuart Dootson2-Dec-09 22:57 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug255-Dec-09 11:47
doug255-Dec-09 11:47 
hi, I made a little test program, to check the performance of a map compared with a vector.

i'm not sure if the method i've used here for key value pairs using a vector is what you meant ? but using a vector for key value look-ups the way i have is much slower than using a map. Here's the test app :

If you know of a way I could improve the performance, i'd be greatful to know ?, thanks

#include "hr_time.h"
#include <map>
#include <algorithm>
#include <vector>
using namespace std;

struct Object
{
	int value;

	Object()
	{
		value = 1;
	};

	~Object(){};
};

map<int, Object> m;
vector<int> ids;
vector<Object> v;

int main()
{
	CStopWatch s;
	double time;
	int i, value;

	// time taken to get the values of
	// 10000 elements with id's using a map

	for(i = 0; i < 10000; i++)
		m.insert(make_pair(i,Object()));

	s.startTimer();

	for(i = 0; i < 10000; i++)
		value = m[i].value;

	s.stopTimer();

	time = s.getElapsedTime();

	// time = 0.061847 seconds

	// time taken to get the values of
	// 10000 elements with id's using a vector

	for(i = 0; i < 9999; i++)
	{
		ids.push_back(i);
		v.push_back(Object());
	}

	ids.push_back(20000); // id of the 10000th element = 20000
	v.push_back(Object());

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

	int pos;

	s.startTimer();

	for(i = 0; i < 9999; i++)
	{
		pos = (int)(lower_bound(ids.begin(), ids.end(), i) - ids.begin());
		value = v[pos].value;
	}

	pos = (int)(lower_bound(ids.begin(), ids.end(), 20000) - ids.begin());
	value = v[pos].value;

	s.stopTimer();

	time = s.getElapsedTime();

	// time = 0.850772 seconds

	return 0;
}

GeneralRe: implementation of a key value array (not using std::map) ? Pin
Stuart Dootson5-Dec-09 11:59
professionalStuart Dootson5-Dec-09 11:59 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug255-Dec-09 15:54
doug255-Dec-09 15:54 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
Stuart Dootson6-Dec-09 0:22
professionalStuart Dootson6-Dec-09 0:22 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug256-Dec-09 13:07
doug256-Dec-09 13:07 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
Stuart Dootson6-Dec-09 13:41
professionalStuart Dootson6-Dec-09 13:41 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug256-Dec-09 14:20
doug256-Dec-09 14:20 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
Stuart Dootson6-Dec-09 14:37
professionalStuart Dootson6-Dec-09 14:37 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug256-Dec-09 15:07
doug256-Dec-09 15:07 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
Stuart Dootson7-Dec-09 22:57
professionalStuart Dootson7-Dec-09 22:57 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug258-Dec-09 3:07
doug258-Dec-09 3:07 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
Stuart Dootson8-Dec-09 5:59
professionalStuart Dootson8-Dec-09 5:59 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug258-Dec-09 17:01
doug258-Dec-09 17:01 
Questionre: probelms erasing a multimap entry Pin
Alan Kurlansky2-Dec-09 9:54
Alan Kurlansky2-Dec-09 9:54 
AnswerRe: re: probelms erasing a multimap entry Pin
«_Superman_»2-Dec-09 12:25
professional«_Superman_»2-Dec-09 12:25 
GeneralRe: re: probelms erasing a multimap entry Pin
Alan Kurlansky3-Dec-09 2:51
Alan Kurlansky3-Dec-09 2:51 
QuestionTransparentBlt doesn't mask transparent color with 4-bit and 8-bit source bitmaps Pin
sashoalm2-Dec-09 8:53
sashoalm2-Dec-09 8:53 
AnswerRe: TransparentBlt doesn't mask transparent color with 4-bit and 8-bit source bitmaps Pin
Richard MacCutchan2-Dec-09 12:01
mveRichard MacCutchan2-Dec-09 12:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.