Poor performance from stdext::hash_map
From: Andy Coates (nothanks_at_nowhere.com)
Date: 06/11/04
- Previous message: tom_usenet: "Re: VC 6.0 to VC 7.0 (version 1.3) porting"
- Next in thread: P.J. Plauger: "Re: Poor performance from stdext::hash_map"
- Reply: P.J. Plauger: "Re: Poor performance from stdext::hash_map"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 11 Jun 2004 12:38:02 +0100
Hi,
I've just noticed that MS are now shipping hash_map and hash_set with
DevStudio. However, I'm finding that I'm getting worse performance than the
standard map container and a lot worse than the MFC CMap. So I'm assuming
I'm doing something wrong!
Here's the code snipet - any help would be greatly appreciated:
void DoMFCSTLCompare()
{
typedef CMap<CString, const TCHAR*, long, long> MFCMap;
typedef std::map<std::string, long> STLMap;
typedef stdext::hash_map<std::string, long> STLExtMap;
const unsigned long nNumValues = 10000;
long l;
CString strMFC;
std::string strSTL;
// Init some strings:
CString strMFC_Init[10000];
std::string strSTL_Init[10000];
for (unsigned long i = 0; i != nNumValues; ++i)
{
strMFC.Format(_T("Hello world %d"), i);
strMFC_Init[i] = strMFC;
strSTL_Init[i] = strMFC;
}
// STL version:
const unsigned long nSTLStartTic = ::GetTickCount();
STLMap stlMap;
for (unsigned long i = 0; i != nNumValues; ++i)
stlMap.insert(std::make_pair(strSTL_Init[i], i));
const unsigned long nSTLInsertEndTic = ::GetTickCount();
const STLMap::const_iterator itEnd = stlMap.end();
for (STLMap::const_iterator it = stlMap.begin(); it != itEnd; ++it)
;
const unsigned long nSTLExtractEndTic = ::GetTickCount();
for (unsigned long i = 0; i != nNumValues; ++i)
assert(stlMap.find(strSTL_Init[i]) != stlMap.end());
const unsigned long nSTLSearchEndTic = ::GetTickCount();
// STLExt version:
const unsigned long nSTLExtStartTic = ::GetTickCount();
STLExtMap stlextMap;
for (unsigned long i = 0; i != nNumValues; ++i)
stlextMap.insert(std::make_pair(strSTL_Init[i], i));
const unsigned long nSTLExtInsertEndTic = ::GetTickCount();
const STLExtMap::const_iterator itEnd2 = stlextMap.end();
for (STLExtMap::const_iterator it2 = stlextMap.begin(); it2 != itEnd2;
++it2)
;
const unsigned long nSTLExtExtractEndTic = ::GetTickCount();
for (unsigned long i = 0; i != nNumValues; ++i)
assert(stlextMap.find(strSTL_Init[i]) != stlextMap.end());
const unsigned long nSTLExtSearchEndTic = ::GetTickCount();
// MFC version:
const unsigned long nMFCStartTic = ::GetTickCount();
MFCMap mfcMap;
mfcMap.InitHashTable(12007);
for (unsigned long i = 0; i != nNumValues; ++i)
mfcMap.SetAt(strMFC_Init[i], i);
const unsigned long nMFCInsertEndTic = ::GetTickCount();
POSITION pos = mfcMap.GetStartPosition();
while (pos != 0)
mfcMap.GetNextAssoc(pos, strMFC, l);
const unsigned long nMFCExtractEndTic = ::GetTickCount();
for (unsigned long i = 0; i != nNumValues; ++i)
assert(mfcMap.Lookup(strMFC_Init[i], l) == TRUE);
const unsigned long nMFCSearchEndTic = ::GetTickCount();
}
If I run this on .Net 2003 I get the following times (Debug build):
nMFCInsertEndTic - nMFCStartTic 94 (MFC
Insert)
nMFCExtractEndTic - nMFCInsertEndTic 16 (MFC Iterate)
nMFCSearchEndTic - nMFCExtractEndTic 41 (MFC Search)
nSTLInsertEndTic - nSTLStartTic 734 (STL
Insert)
nSTLExtractEndTic - nSTLInsertEndTic 16 (STL
Iterate)
nSTLSearchEndTic - nSTLExtractEndTic 235 (STL Search)
nSTLExtInsertEndTic - nSTLExtStartTic 2531 (STLExt
Insert)
nSTLExtExtractEndTic - nSTLExtInsertEndTic 0 (STLExt
Iterate)
nSTLExtSearchEndTic - nSTLExtExtractEndTic 11687 (STLExt Search)
If I remove the InitHashMap call for the MFC version I get a slightly higher
time, but still much quicker than the std::map (as expected as CMap is a
hash container). What does puzzel me is the hash_map times.
I'm knocking up a coding standards document and I'm a big fan of the STL, so
I'd like to be able to recommend the STL containers over their MFC
conterparts; but as it stands I won't be able to do that.
Thanks for any help,
Andy
- Previous message: tom_usenet: "Re: VC 6.0 to VC 7.0 (version 1.3) porting"
- Next in thread: P.J. Plauger: "Re: Poor performance from stdext::hash_map"
- Reply: P.J. Plauger: "Re: Poor performance from stdext::hash_map"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|