問題描述
我基本上是在準備要放入數據庫的短語,它們可能格式不正確,所以我想存儲它們的短散列(我將簡單地比較它們是否存在,因此散列是理想的).
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 exist or not, so hash is ideal).
我認為 MD5 在 100,000 多個請求上相當慢,所以我想知道散列短語的最佳方法是什么,也許推出我自己的散列函數或使用 hash('md4', '...'
最終會更快嗎?
I assume MD5 is fairly slow on 100,000+ requests so I wanted to know what would be the best method to hash the phrases, maybe rolling out my own hash function or using hash('md4', '...'
would be faster in the end?
我知道 MySQL 有 MD5(),所以這會在查詢端增加一點速度,但也許 MySQL 中還有一個更快的散列函數,我不知道它是否適用于 PHP..
I know MySQL has MD5(), so that would complement a bit of speed on the query end, but maybe there's further a faster hashing function in MySQL I don't know about that would work with PHP..
推薦答案
CRC32 非常快,并且有一個函數:http://www.php.net/manual/en/function.crc32.php
CRC32 is pretty fast and there's a function for it: http://www.php.net/manual/en/function.crc32.php
但是您應該知道 CRC32 比 MD5 甚至 SHA-1 哈希具有更多的沖突,這僅僅是因為長度減少了(32 位與 128 位和 160 位相比).但是,如果您只想檢查存儲的字符串是否已損壞,那么使用 CRC32 就可以了.
But you should be aware that CRC32 will have more collisions than MD5 or even SHA-1 hashes, simply because of the reduced length (32 bits compared to 128 bits respectively 160 bits). But if you just want to check whether a stored string is corrupted, you'll be fine with CRC32.
這篇關于非加密用途的最快哈希?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!