moderated >> stdext::hashmap and std::map are different perfermance in Multi Threaded Base

by metdoloca » Thu, 02 Jul 2009 04:23:58 GMT

in <multi threaded> programming( maybe threadcount is 100 )

1. map
std::map< long,DATA > testMap[100];
LockObject lock[100];

ThreadProc()
{

long lSerial = rand();
long lHashSerial = lSerial%100;

DATA sData;

// LockStart
{
LockObejct lock( lock[100] );
testMap[lHashSerial] = sData
}
// LockEnd
}

2. hashmap
stdext::hashmap< long, DATA > testHashMap;
LockObject lock;

ThreadProc()
{

long lSerial = rand();

DATA sData;

// LockStart
{
LockObejct lock( lock );
testHashMap[lSerial] = sData
}
// LockEnd
}


which is more perfermance?

1 or 2

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]



moderated >> stdext::hashmap and std::map are different perfermance in Multi Threaded Base

by George Neuner » Thu, 02 Jul 2009 10:47:55 GMT






Example 1 should not even compile ... you are trying to assign a DATA
object to a map instead of inserting the data object into the map. You
are also synchronizing all threads on a single lock which is probably
not what you intended given that you have 100 maps.

I don't know what this LockObject is that you are using, but modulo
correct use of it, the code for this example should probably be
something like:

LockObject lock( lock[lHashSerial] )
testMap[lHashSerial][lSerial] = sData;

or

lock[lHashSerial].Lock();
testMap[lHashSerial][lSerial] = sData;
lock[lHashSerial].Unlock();




A hash_map is generally much faster than a map, although it _could_
potentially be slower depending on the complexity of the optional
custom hash and comparison functions (your hash_map example uses the
default versions).

However, I'm not sure you really know what you are asking because your
examples are not functionally equivalent. Your map code uses multiple
maps whereas your hash_map code uses a single map. Are you asking
about the relative performance of std::map vs stdext::hash_map or the
relative performance of using a single map vs multiple maps?

George

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]




moderated >> stdext::hashmap and std::map are different perfermance in Multi Threaded Base

by Andrew Tomazos » Fri, 03 Jul 2009 03:47:08 GMT




Go and study Binary Trees and Hash Tables. A decent book is:

Introduction To Algorithms
Cormen, Leiserson, Rivest, Stein.
http://bit.ly/KLZ51

Chapter 11 and 12.

Enjoy,
Andrew.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]



Similar Threads

1. stdext::hashmap and std::map are different perfermance in Multi Threaded Base

2. How to write a multi value compare function for std::map

3. Multi-threaded Debug DLL works / not Multi-threaded DLL

Ok so I can release my app when using the following Runtime Library

-> Multi-threaded Debug DLL
and I get the app to crash right on execution when using any other
settings for The Runtime Library

"I do realize that its not alowed to release an application using
Multi-threaded Debug DLL"

when I run the release version using  Runtime Library

-> Multi-threaded DLL

the application crashs when I try to use my COM port to connect to a
certain device...
So when I debug this error
I get pointed to this pice of code:

	/* Output the data to the serial port */
-->    CByteArray byteArray;
	for ( k=0; k<uwIndex; k++ )
	{
		m_pResourceMgr->AddToMessageBuffer( ucMessage[k] );
		byteArray.Add( (BYTE)ucMessage[k] );
	}

I'm not sure that would help but if you know any tips I could use to
fix this problem would be greatly appressiated!

Thanks,
Jonathan

4. std::maps within std::maps -- optimisation

5. Problems with std::map.insert() and std::map.find()

I have written a class template to store manage/raw pointers, I want to 
store pointers to an obects, using a key. I find that find() is not 
working (after the first insert) - i.e. once the first item is inserted 
in the map, any key passed to the find() method will return the first item.

Additionally, once the first item has been stored, no additional items 
can be stored - its driving me nuts, and I cant work outt why this is 
happening. I have included both my classes (i.e class template and key 
class here - hopefully, someone can spot what is causing things to go 
awry). To summarize the problem - every - i.e. find() and insert() work 
as expected, UNTIL the first item is inserted into the map - which seems 
to suggest that somehow, the Keys are considered non-unique???

The code follows below:

PointerMap.h
=============

#pragma once

#include <string>
#include <map>
#include <stdexcept>


template<class T1, class T2>
class PointerMap
{
public:
	typedef typename std::map<T1, T2*>::reference reference;
	typedef typename std::map<T1, T2*>::const_reference const_reference;
	typedef typename std::map<T1, T2*>::const_iterator const_iterator;
	typedef typename std::map<T1, T2*>::iterator iterator;

	PointerMap()
	{
	}

	~PointerMap()
	{
		clear();
	}

	iterator begin() { return m_map.begin(); }
	iterator end() { return m_map.end(); }
	const_iterator begin() const { return m_map.begin(); }
	const_iterator end() const { return m_map.end(); }
	iterator find(T1 key) { return m_map.find(key) ; }
	const_iterator find(T1 key) const { return m_map.find(key) ; }


	iterator erase(iterator where)
	{
		if(where != m_map.end() && *where)
		{
			T*& ptr = *where;
			delete ptr;
			ptr = 0;
		}
		
		return m_map.erase(where);
	}

	iterator erase(iterator begin, iterator end)
	{
		while(begin != end)
		{
			T2*& ptr = begin->second ;
			if ( ptr != 0)
			{
				delete ptr ;
				ptr = 0;
			}
			
			begin++;
		}

		return m_map.erase(begin, end);
	}

	void clear() { erase(begin(), end()); }

	bool empty() const { return m_map.empty(); }
	size_t size() const { return m_map.size(); }

	void insert(T1 key, T2* val)
	{
		m_map.insert(std::pair<T1, T2*>(key, val));
	}

	T2* operator[] (const T1 idx) const
	{
		if (idx > m_map.size())
			throw std::logic_error("Array bounds exceeded");
		else
			return m_map[idx] ;
	}
	
	T2*& operator[] (const T1 idx)
	{
		if (idx > m_map.size())
			throw std::logic_error("Array bounds exceeded");
		else
		{
			T2*& ptr = m_map[idx] ;
			if (ptr)
			{	
				delete ptr ;
				ptr = 0 ;
			}
			return m_map[idx] ;
		}
	}

private:
	PointerMap(const PointerMap& source);
	PointerMap& operator=(const PointerMap& source);

	std::map<T1, T2*> m_map;
};


RepositoryKey.h
================

class RepositoryKey
{
public:
	RepositoryKey(const unsigned char xch, const long ic, const 
std::string& syb, const long fq = 0):
		m_xch(xch), m_ic(ic), m_syb(syb), m_fq(fq)
	{
	}

	RepositoryKey(const RepositoryKey& key):
		m_xch(key.m_xch), m_syb(key.m_syb), m_ic(key.m_ic), m_fq(key.m_fq)
	{
	}

	~RepositoryKey()
	{
	}

	RepositoryKey& operator=(const RepositoryKey& key)		
	{
		if (this != &key)
		{
			m_xch = key.m_xch;
			m_syb = key.m_syb;
			m_ic = key.m_ic ;
			m_fq = key.m_fq ;
		}
		return *this;
	}

	bool operator==(const RepositoryKey& key) const
	{
		return ( (m_xch == key.m_xch) && (m_syb == key.m_syb) &&
				 (m_ic == key.m_ic) && ((long)m_fq == (long)key.m_fq) );
	}

	bool operator < (const RepositoryKey& key) const
	{
		return ( (m_xch < key.m_xch) && (m_ic < key.m_ic) && ((long)m_fq < 
(long)key.m_fq)
				&& (_stricmp(m_syb.c_str(), key.m_syb.c_str()) < 0));
	}

	unsigned char Id() const { return m_xch; }
	long Ic() const { return m_ic; }
	std::string Symbol() const { return m_syb; }
	long Freq() const { return m_fq ; }

private:
	unsigned char m_xch ;
	long m_ic ;
	std::string m_syb ;
	long m_fq;
};


6. Multi Threaded Eg in Doc/View and Dialog Based - VC MFC

7. HashMap vs Tree Map (was counting counting)

8. Multi-thread OR Event-based?