"Christian Christmann" <pl*****@yahoo.dewrote in message
news:44***********************@newsread4.arcor-online.net...
Hi,
I've profiled one of my C++ projects and figured out that the
program spends a lot of time with STL map's function "find".
One of my classes possesses an STL map of the structure
map< string, string myMap;
The function that consumes a substantial fraction of the
program execution, searches in the map:
string findString( const char *lab )
{
string tmp( lab );
map< string, string >::const_iterator it = myMap.find( tmp.c_str() );
if ( it != myMap.end() ) {
string myString( it->second.c_str() );
return myString;
}
// Otherwise
string myString( lab );
return myString;
}
Do you see any possibility to optimize the "find" function on "myMap"?
you could use what I call a string-tree:
template<class T>
class CStringTree
{ private:
std::auto_ptr<CStringTreem_sChildren[256];
T m_s;
public:
void addEntry(const char *_p, const T &_r)
{ if (*_p)
{ if (!m_sChildren[*_p].get())
m_sChildren[*_p] = std::auto_ptr<CStringTree>(new
CStringTree);
m_sChildren[*_p].get()->addEntry(_p + 1, _r);
}
else
{ if (!m_sChildren[0].get())
m_sChildren[0] = std::auto_ptr<CStringTree>(new
CStringTree);
m_sChildren[0].get()->m_s = _r;
}
}
const T *findEntry(const char *_p)
{ if (*_p)
if (!m_sChildren[*_p].get())
return 0;
else
return m_sChildren[*_p].get()->findEntry(_p + 1);
else
if (!m_sChildren[0].get())
return 0;
else
return &m_sChildren[0].get()->m_s;
}
};
Creating an entry in a string tree may take more time than creating an entry
in a map.
But finding an entry is considerable faster.