C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

efficiency of map::operator[] and map-insert

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
gast128@hotmail.com
Guest





PostPosted: Sat Jun 03, 2006 3:37 am    Post subject: efficiency of map::operator[] and map-insert Reply with quote



Dear all,

item 24 of Scott Meyers (excellent) effective STL book is about the
performance difference of operator[] compared to map insert. In short
he warns about the possible performance loss of operator[] due to an
extra copy.

Our code uses a lot of this constructs and therefore I profiled his
efficient update algorithm with a std::map<int, std::sting>.

I come to the following profile results (Visual Studio 2003 release
builds):

only adds:
operator[] 15.1
Scott 12.5
(+/- 20%)

dominating inserts:
operator[] 26.4
Scott 25.0
(+/- 4%)

Thus my guess will be that indeed only a very heavy object with
dominating adds in a container will make a difference.

code used:
namespace
{
typedef std::map<int, std::string> Int2StringMap;

const size_t g_nMax = 100000;
const size_t g_nTimes = 10;
//const size_t g_nTimes = 1;
const std::string g_str1 = _T("Hello");
const std::string g_str2 = _T("Hello world");

template <typename MapType, typename KeyType, typename ValueType>
typename MapType::iterator map_add_or_update(MapType& rMap, const
KeyType& rKey, const ValueType& rValue)
{
typename MapType::iterator it = rMap.lower_bound(rKey);
if (it != rMap.end() && (rMap.key_comp()(rKey, it->first)) ==
false)
{
//update
it->second = rValue;
return it;
}
else
{
return rMap.insert(it, MapType::value_type(rKey, rValue));
}
}
}


//profiler does not see static fct's
void Prf1aImpl(Int2StringMap* pMap);
void Prf1bImpl(Int2StringMap* pMap);


//----------------------------------------------------------------------------
// Function Prf1
//----------------------------------------------------------------------------
// Description : prf1
//----------------------------------------------------------------------------
void Prf1()
{
Int2StringMap mp1a;
Int2StringMap mp1b;

Prf1aImpl(&mp1a);
Prf1bImpl(&mp1b);

_ASSERT(mp1a == mp1b);
}


//----------------------------------------------------------------------------
// Function Prf1aImpl
//----------------------------------------------------------------------------
// Description : use operator[]
//----------------------------------------------------------------------------
void Prf1aImpl(Int2StringMap* pMap)
{
for (int n = 0; n < g_nTimes; ++n)
{
const std::string& cr = ((n % 2 == 0) ? g_str1 : g_str2);

for (int i = 0; i < g_nMax; ++i)
{
(*pMap)[i] = cr;
}
}
}


//----------------------------------------------------------------------------
// Function Prf1bImpl
//----------------------------------------------------------------------------
// Description : use Scott Meyers impl.
//----------------------------------------------------------------------------
void Prf1bImpl(Int2StringMap* pMap)
{
for (int n = 0; n < g_nTimes; ++n)
{
const std::string& cr = ((n % 2 == 0) ? g_str1 : g_str2);

for (int i = 0; i < g_nMax; ++i)
{
map_add_or_update(*pMap, i, cr);
}
}
}

Wkr me.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Bo Persson
Guest





PostPosted: Sat Jun 03, 2006 7:13 pm    Post subject: Re: efficiency of map::operator[] and map-insert Reply with quote



<gast128 (AT) hotmail (DOT) com> skrev i meddelandet
news:1149176899.591167.293850 (AT) u72g2000cwu (DOT) googlegroups.com...
Quote:
Dear all,

item 24 of Scott Meyers (excellent) effective STL book is about the
performance difference of operator[] compared to map insert. In
short
he warns about the possible performance loss of operator[] due to an
extra copy.

Our code uses a lot of this constructs and therefore I profiled his
efficient update algorithm with a std::map<int, std::sting>.

I come to the following profile results (Visual Studio 2003 release
builds):

only adds:
operator[] 15.1
Scott 12.5
(+/- 20%)

dominating inserts:
operator[] 26.4
Scott 25.0
(+/- 4%)

Thus my guess will be that indeed only a very heavy object with
dominating adds in a container will make a difference.


I belive the reason is that the C++ committee has decided to change
the specification of operator[]

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#334

and this change is already implemented in the VS 2003 library.


Bo Persson



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.