问题描述
我使用一个带有 std :: string
键的地图,当一切正常工作时,我没有得到我预期的性能。我搜索的地方,以优化和改进的东西只有一点,这是一个同事说,字符串键将是缓慢的。
我读了几十个问题他们一贯说:
I reluctantly tried a char *
key and there was a difference, a big difference.
I boiled the problem down to a simple example:
#include <stdio.h>
#include <stdlib.h>
#include <map>
#ifdef USE_STRING
#include <string>
typedef std::map<std::string, int> Map;
#else
#include <string.h>
struct char_cmp {
bool operator () (const char *a,const char *b) const
{
return strcmp(a,b)<0;
}
};
typedef std::map<const char *, int, char_cmp> Map;
#endif
Map m;
bool test(const char *s)
{
Map::iterator it = m.find(s);
return it != m.end();
}
int main(int argc, char *argv[])
{
m.insert( Map::value_type("hello", 42) );
const int lcount = atoi(argv[1]);
for (int i=0 ; i<lcount ; i++) test("hello");
}
First the std::string version:
$ g++ -O3 -o test test.cpp -DUSE_STRING
$ time ./test 20000000
real 0m1.893s
Next the 'char *' version:
g++ -O3 -o test test.cpp
$ time ./test 20000000
real 0m0.465s
That's a pretty big performance difference and about the same difference I see in my larger program.
Using a char *
key is a pain to handle freeing the key and just doesn't feel right. C++ experts what am I missing? Any thoughts or suggestions?
You are using a const char *
as a lookup key for find()
. For the map containing const char*
this is the correct type that find
expects and the lookup can be done directly.
The map containing std::string
expects the parameter of find()
to be a std::string
, so in this case the const char*
first has to be converted to a std::string
. This is probably the difference you are seeing.
这篇关于C ++ map< std :: string> vs地图< char *>性能(我知道,“再次?”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!