### rifat1234's blog

By rifat1234, 6 years ago, ,

Can anyone please tell me which one is faster among those two ( map<const char * ,int> or map<string,int> ) ?

• +3

 » 6 years ago, # |   +46 The first one is definetely faster, as comparasion takes constant time.But it doesn't work in the way you want. C-style strings are not strings — they're pointers to a memory where string data lies (with terminating '\0'). That's why you shouldn't write char *a = "foo", *b = "bar"; if (a < b) // ... — here pointers (that is, memory addresses) are compared, not the string themselves.You should always use strcmp when dealing with C-strings. map<> doesn't know about it, and it will compare addresses, not strings. That can lead to a very strange behavior, don't do that, use std::string instead — it behaves more like a normal string (you can compare, modify, copy and concatenate them easily)
 » 6 years ago, # | ← Rev. 3 →   +19 In pure speed, I'll vote for const char* BUT you should be careful using it map T; int main () { T["abc"] = 1; cout << T["abc"]; }  Output is 1, correct map T; int main () { char temp[] = "abc"; T[temp] = 1; cout << T["abc"]; }  Output is 0, WRONG!
•  » » 6 years ago, # ^ |   -8 why so many negatives to Lazycoder97 post! its a good one.
 » 6 years ago, # |   +5 I'm not sure how it really works. In that case Lazycoder97 gave, what is happening when we have map T and we write T["abc"] = 1; ? const char* is only a pointer, so I will think that some place in memory is allocated and "abc" is written here and a pointer to this place is returned, but we are not able to access it. But if it had worked in that way, cout<
•  » » 6 years ago, # ^ |   +43 Map doesn't. It's compiler's optimization — it can store similar const data in the same place.
•  » » » 6 years ago, # ^ |   0 :o If compiler changes an execution of program that is for me sufficient reason to never use it :P.
•  » » » » 6 years ago, # ^ |   +13 Then you should always use -O0 and inline assembly :)Optimizer and C++ standard can do evil things together. You shouldn't assume anything until you're absolutely sure. For example, it's known that arguments of function can be calculated in arbitrary order.
 » 6 years ago, # |   +11 I've tested on 1kk prepared random a-z strings 1kk inserts into map and map: struct cstr_less{ bool operator ()(char *a, char *b) const { return strcmp(a, b) < 0; } }; For strings shorter than 4 chars and longer than 15 map slightly outperforms map because of absense of copying, and for 4-15 chars map faster because of std::string cache locality for length <16 bytes in Dinkumware STL. And, of course, for >16 chars inserts into char*-map don't rely on string length (due to fixed N of random strings E(first diff char in comparator) ~ const), but into string-map do.
•  » » 6 years ago, # ^ |   0 Would you mind testing it with C++11 enabled? It has move semantics, which should throw out copyings and improve performance of map
•  » » » 6 years ago, # ^ |   +14 There is no such thing as C++11, there are C++ and C++ ISO/IEC 14882:2003. One can use pre-made of strings and then emplace it in map, or use boost::string_ref for wrapping char* with std::string-like interface.