Is it possible to map string to int faster than using hashmap?

前端 未结 7 1280
温柔的废话
温柔的废话 2020-12-08 19:52

I understand that I should not optimize every single spot of my program so please consider this question to be \"academic\"

I have maximum 100 strings and integer n

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 20:27

    Well, you could store the strings in a binary tree and search there. While this has O(log n) theoretical performance, it may be a lot faster in practise if you only have a few keys, that are really long, and that already differ in the first few characters.

    I.e. when comparing keys is cheaper than computing the hash function.

    Furthermore, there are CPU caching effects and such that may (or may not) be beneficial.

    However, with a fairly cheap hash function, the hash table will be hard to beat.

提交回复
热议问题