Can anyone please tell me which one is faster among those two ( map<const char * ,int> or map<string,int> ) ?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
Название |
---|
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, usestd::string
instead — it behaves more like a normal string (you can compare, modify, copy and concatenate them easily)In pure speed, I'll vote for const char* BUT you should be careful using it
why so many negatives to Lazycoder97 post! its a good one.
I'm not sure how it really works. In that case Lazycoder97 gave, what is happening when we have map<const char*, int> 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<<T["abc"]; wouldn't have printed 1. How map knows that those "abc"'s from T["abc"] = 1 and cout<<T["abc"]; are the same "abc"'s?
Map doesn't. It's compiler's optimization — it can store similar const data in the same place.
:o If compiler changes an execution of program that is for me sufficient reason to never use it :P.
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.
I've tested on 1kk prepared random a-z strings 1kk inserts into map<char *, int, cstr_less> and map<string, int>:
For strings shorter than 4 chars and longer than 15 map<char*, int> slightly outperforms map<string, int> because of absense of copying, and for 4-15 chars map<string, int> 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.
Would you mind testing it with C++11 enabled? It has move semantics, which should throw out copyings and improve performance of map
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.