Search code examples
c++stringintegerunordered-mapunordered-set

Is there any difference in perofrmance of unordered_set (C++) in case of strings vs in case of integer?


I was wondering that unordered_set uses hashing, so that should be faster in the case of integers than in the case of strings. The same would be the case for unordered_map. I found no definite answer on the web. It will be great if someone can clarify this.


Solution

  • Is there any difference in perofrmance of unordered_set (C++) in case of strings vs in case of integer?

    There can be. The language specification doesn't have guarantees one way or the other.

    You can verify whether this is the case for your program on your target system by measuring the performance.


    If you're considering whether to use string itself as the key, or a separately hashed string (i.e. integer), then technically separate hashing is more expensive since the integer would be hashed again. That said, hashing an integer is trivial (I think it may be the identity function), so this might have no noticeable effect.

    Separate hashing + storing integer does have potential advantage: You can pre-hash the string keys once, and reuse the hashed integer key, while a map with string keys requires the key to be re-hashed on every lookup. Whether this is useful in your case depends on what you're going to do with the map.