Fastest hash for non-cryptographic uses?

后端 未结 13 1125
挽巷
挽巷 2020-12-04 08:23

I\'m essentially preparing phrases to be put into the database, they may be malformed so I want to store a short hash of them instead (I will be simply comparing if they exi

13条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 08:28

    Caveat

    The answer below does not answer the question as asked, since it does not recommend hash functions. Remember, "A hash function is any function that can be used to map data of arbitrary size to fixed-size values." (Wikipedia) The answer below recommends transformations that do not guarantee fixed-size results.

    If you are willing to relax the requirement of using a hash function, read on...

    Original Answer

    I suggest urlencode() or base64_encode() for these reasons:

    • You don't need cryptography
    • You want speed
    • You want a way to identify unique strings while cleaning up 'malformed' strings

    Adapting the benchmark code elsewhere in these replies, I've demonstrated that either of these are way faster than any hash algorithm. Depending on your application, you might be able to use urlencode() or base64_encode() to clean up any 'malformed' strings you want to store.

提交回复
热议问题