Cost of using std::map with std::string keys vs int keys?

前端 未结 6 1647
礼貌的吻别
礼貌的吻别 2020-12-06 01:43

I know that the individual map queries take a maximum of log(N) time. However I was wondering, I have seen a lot of examples that use strings as map keys. What is the perfor

6条回答
  •  春和景丽
    2020-12-06 02:15

    In addition to the time complexity from comparing strings already mentioned, a string key will also cause an additional memory allocation each time an item is added to the container. In certain cases, e.g. highly parallel systems, a global allocator mutex can be a source of performance problems.

    In general, you should choose the alternative that makes the most sense in your situation, and only optimize based on actual performance testing. It's notoriously hard to judge what will be a bottleneck.

提交回复
热议问题