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

后端 未结 14 977
天涯浪人
天涯浪人 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条回答
  •  情书的邮戳
    2021-02-05 23:54

    You can try to use a sorted vector (here's one sample), this may turn out to be faster (you'll have to profile it to make sure of-course).

    Reasons to think it'll be faster:

    1. Less memory allocations and deallocations (the vector will expand to the maximal size used and then reuse freed memory).
    2. Binary find with random access should be faster than tree traversal (espacially due to data locality).

    Reasons to think it'll be slower:

    1. Deleations and additions will mean moving strings around in memory, since string's swap is efficiant and the size of the data set is small this may not be an issue.

提交回复
热议问题