C ++ map< std :: string> vs地图< char *>性能(我知道,“再次?”) [英] C++ map<std::string> vs map<char *> performance (I know, "again?")
问题描述
我使用一个带有 std :: string
键的地图,当一切正常工作时,我没有得到我预期的性能。我搜索的地方,以优化和改进的东西只有一点,这是一个同事说,字符串键将是缓慢的。
我读了几十个问题他们一贯说:
不要使用
char *
键
std :: string
键从不是您的瓶颈
char *
和a
std :: string
是神话。
我不情愿尝试一个 char *
键,有一个区别,有很大的区别。
我把问题归结为一个简单的例子:
include< stdio.h>
#include< stdlib.h>
#include< map>
#ifdef USE_STRING
#include< string>
typedef std :: map< std :: string,int>地图;
#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>地图;
#endif
映射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 }
首先是std :: string版本:
$ g ++ -O3 -o test test.cpp -DUSE_STRING
$ time ./test 20000000
real 0m1.893s
接下来是'char *'版本:
g ++ -O3 -o test test.cpp
$ time ./test 20000000
real 0m0.465s
这是一个非常大的性能差异,我在大型程序中看到的差别很大。
使用 char *
键是一个痛苦的处理释放的关键,只是不觉得正确。 C ++专家我缺少什么?任何想法或建议?
您使用的是 const char *
作为 find()
的查找键。对于包含 const char *
的映射,这是 find
期望的正确类型,并且可以直接进行查找。 / p>
包含 std :: string
的映射需要 find()
是 std :: string
,因此在这种情况下, const char *
转换为 std :: string
。这可能是您看到的差异。
I was using a map with a std::string
key and while everything was working fine I wasn't getting the performance I expected. I searched for places to optimize and improved things only a little and that's when a colleague said, "that string key is going to be slow."
I read dozens of questions and they consistently say:
"don't use a
char *
as a key"
"std::string
keys are never your bottleneck"
"the performance difference between achar *
and astd::string
is a myth."
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 *>性能(我知道,“再次?”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!