C++ map vs map performance (I know, “again?”)

前端 未结 6 1936
北恋
北恋 2020-12-24 11:05

I was using a map with a std::string key and while everything was working fine I wasn\'t getting the performance I expected. I searched for places to optimize

6条回答
  •  眼角桃花
    2020-12-24 11:35

    After compilation the 2 "Hello" string literals will have the same memory address. On the char * case you use this memory addresses as keys.

    In the string case every "Hello"s will be converted to a different object. This is a small part (really really small) of your performance difference.

    A bigger part can be that as all the "Hello"s you are using has the same memory address strcmp will always get 2 equivalent char pointers and I'm quite sure that it early checks for this case :) So it will never really iterate on the all characters but the std::string comparison will.

提交回复
热议问题