How can I increase the performance in a map lookup with key type std::string?

后端 未结 14 976
天涯浪人
天涯浪人 2021-02-05 23:25

I\'m using a std::map (VC++ implementation) and it\'s a little slow for lookups via the map\'s find method.

The key type is std::string.

14条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-06 00:03

    Motti has a good solution. However, I'm pretty sure that for your < 15 elements a map isn't the right way because its overhead will always be greater than that of a simple lookup table with an appropriate hashing scheme. In your case, it might even be enough to hash by length alone, and if that still produces collisions, use a linear search through all entries of the same length.

    To establish if I'm right, a benchmark is of course required but I'm quite sure of its outcome.

提交回复
热议问题