473,581 Members | 3,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optimization of STL map's find

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_iterat or 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"?

And in general, do you have any suggestions how to improve the function
"findString "?

Regards,
Chris
Aug 6 '06 #1
7 6090
Christian Christmann wrote:
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_iterat or it = myMap.find( tmp.c_str() );
map< string, string >::const_iterat or it = myMap.find( tmp );
>
if ( it != myMap.end() ) {
string myString( it->second.c_str () );
return myString;
return it->second;
}

// Otherwise
string myString( lab );
Why again?

return tmp;
>
return myString;
}
Do you see any possibility to optimize the "find" function on "myMap"?
You could try a different comparison predicate: if many of your string have
a common prefix, lexicographic comparison may look at many characters
before being able to decide. In this case, short-lex order could take
advantage of strings being of different lengths.

Also, if your map is more or less static, i.e., built once and used
afterward for searching only, you could use

std::vector< std::pair< std::string, std::string

instead: put all pairs in there, sort, and do table lookup by binary search.
However, that should not make a big difference.

Another option is to use an unordered_map instead where you can play around
with a hash function.
And in general, do you have any suggestions how to improve the function
"findString "?
See above: I wondered why you are going through c_str(). Your map knows
strings, just use them.
Best

Kai-Uwe Bux
Aug 6 '06 #2
Christian:

Try implementing something like this:

//your map deffinition (take care of the memory!!!)
typedef map< string, string* my_map_t;

//your function deffinition
const string & findString( const string & lab );
const string & findString( const char * lab );

This way, the map::find funciton itself will execute more efficiently,
and you wont have to dereference the pointer or create a temp object in
your function, just return a reference to the map's string.

Hope this help.
Christian Christmann wrote:
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_iterat or 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"?

And in general, do you have any suggestions how to improve the function
"findString "?

Regards,
Chris
Aug 6 '06 #3

flagos wrote:
Christian:

Try implementing something like this:

//your map deffinition (take care of the memory!!!)
typedef map< string, string* my_map_t;

//your function deffinition
const string & findString( const string & lab );
const string & findString( const char * lab );

This way, the map::find funciton itself will execute more efficiently,
and you wont have to dereference the pointer or create a temp object in
your function, just return a reference to the map's string.

Hope this help.
Coud you please explain why this way helps memory? And, how?

Michael

Aug 6 '06 #4
Christian Christmann wrote:
string findString( const char *lab )
{
string tmp( lab );

map< string, string >::const_iterat or it = myMap.find( tmp.c_str() );

if ( it != myMap.end() ) {
string myString( it->second.c_str () );
return myString;
}

// Otherwise
string myString( lab );

return myString;
}
Try this:
inline string findString( const char *lab )
{
map< string, string >::const_iterat or it = myMap.find(lab) ;

if ( it != myMap.end() ) {
return it->second;
}

// Otherwise
return lab;
}

If this makes compile errors, try this:
inline string findString( const char *lab )
{
map< string, string >::const_iterat or it = myMap.find(stri ng(lab));

if ( it != myMap.end() ) {
return it->second;
}

// Otherwise
return string(lab);
}

Regards
Thorsten

Aug 6 '06 #5
Christian Christmann wrote:
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_iterat or it = myMap.find( tmp.c_str() );
I'm confused as to why you make a local string object here and then
convert back to char* to then pass it back to something expecting a
string.
>
if ( it != myMap.end() ) {
string myString( it->second.c_str () );
return myString;
Again, why the temporary? etc...
}

// Otherwise
string myString( lab );

return myString;
}
You might want to consider putting the string in the map. in the
case it's not found. Any how, the below is a more succint way
of expressing the exact same thing (without all the extra copies).

string findString(cons t char* lab) {
map<string, string>::const_ iterator it = myMap.find(lab) ;
if(it != myMap.end()
return it->second;
else
return lab;
}
Aug 6 '06 #6
If this makes compile errors, try this: inline string findString( const
char *lab ) {
map< string, string >::const_iterat or it = myMap.find(stri ng(lab));

if ( it != myMap.end() ) {
return it->second;
}
}
// Otherwise
return string(lab);
}
}
Just another idea: In order to avoid calling string's constructor, one
could return a reference:

inline const string& findString( const string&lab ) {
map< string, string >::const_iterat or it = myMap.find(lab) ;

if ( it != myMap.end() ) {
return it->second;
}

// Otherwise
return lab
}


Aug 6 '06 #7

"Christian Christmann" <pl*****@yahoo. dewrote in message
news:44******** *************** @newsread4.arco r-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_iterat or 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<C StringTreem_sCh ildren[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<C StringTree>(new
CStringTree);

m_sChildren[*_p].get()->addEntry(_p + 1, _r);

}

else

{ if (!m_sChildren[0].get())

m_sChildren[0] = std::auto_ptr<C StringTree>(new
CStringTree);

m_sChildren[0].get()->m_s = _r;

}

}

const T *findEntry(cons t 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.

Aug 6 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1528
by: Dave | last post by:
Hello all, I have written an expression interpreter, and a profiler has told me that a specific part of it is in need of optimization. My purpose in posting to this newsgroup is to solicit suggestions on how I might accomplish my intended aim more efficiently. My interpreter supports the "if-then-else" construct. In addition, it...
3
1428
by: mcassiani | last post by:
Hi, I need use map faster as possible (I store in the map data about open network connections). First a question, this code fragment is from "The C++ Programming........
10
4560
by: MariusI | last post by:
I stumbled over an optimization (or lack of one, to be specific) when viewing IL opcodes generated by the compiler using ms .net 2003. I was testing fast pixel manipulation using Bitmap.LockBits and unsafe pointers to iterate over an image's pixels. The inner-loop looked like this: for (int x = 0; x < _buffer.Width*_buffer.Height; x++) {...
3
1465
by: Jazzkt | last post by:
I wrote a little code using the map container available in the STL of C++. The trouble I am having is that the find() member function is working properly. When I try to find a key, if that key is not in the map, it should return the iterator set to the address of the end of the map. However, when I try to find a key in the map, it returns a...
4
1424
by: Mervin Williams | last post by:
I am building a web application that will have a single form that will be populated with data from 8-10 tables. The easiest way to implement the data tier would be to use DataSets and Data Adapters for each table. But, is that the optimal approach? How do I optimize the back-end data retrieval and corresponding updates (inserts, updates,...
0
1094
by: Ole Nielsby | last post by:
I just wonder if this is possible with C++/clr. I'm trying to .NET-enable an interpreter for a Lisp flavoured language, the interpreter written in NASM which I have managed to port to MASM. However the VS2005 debugger is giving me a really hard time - seems it doesn't cope well with mixing MASM with C++/clr, so I am considering a rewrite...
3
1257
by: telnet2008 | last post by:
Hi,everyone: the questionly code: #include <iostream> using namespace std; class test { int x;
6
1901
by: wqe | last post by:
Hi,everyone: the questionly code: #include <iostream> using namespace std; class test { int x;
6
1800
by: baudolino | last post by:
I have a map<string, time_t>; string represents a filesystem path, time_t is a timestamp associated with that path. My problem is to find the best version of a directory and its subdirectories, regarding its timestamp. For example, if I have: /home, 1000 /home/user1, 2000 the best version is (/home, 1000) AND (/home/user1, 2000) ( because...
0
7886
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7809
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8312
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7920
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5685
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.