- Function DJBHash ($ str) // 0.22
- {
- $ Hash = 0;
- $ N = strlen ($ str );
- For ($ I = 0; $ I <$ n; $ I ++)
- {
- $ Hash + = ($ hash <5) + ord ($ str [$ I]);
- }
- Return $ hash % 701819;
- }
- Function ELFHash ($ str) // 0.35
- {
- $ Hash = $ x = 0;
- $ N = strlen ($ str );
- For ($ I = 0; $ I <$ n; $ I ++)
- {
- $ Hash = ($ hash <4) + ord ($ str [$ I]);
- If ($ x = $ hash & 0xf0000000 )! = 0)
- {
- $ Hash ^ = ($ x> 24 );
- $ Hash & = ~ $ X;
- }
- }
- Return $ hash % 701819;
- }
- Function JSHash ($ str) // 0.23
- {
- $ Hash = 0;
- $ N = strlen ($ str );
- For ($ I = 0; $ I <$ n; $ I ++)
- {
- $ Hash ^ = ($ hash <5) + ord ($ str [$ I]) + ($ hash> 2 ));
- }
- Return $ hash % 701819;
- }
- Function SDBMHash ($ str) // 0.23
- {
- $ Hash = 0;
- $ N = strlen ($ str );
- For ($ I = 0; $ I <$ n; $ I ++)
- {
- $ Hash = ord ($ str [$ I]) + ($ hash <6) + ($ hash <16)-$ hash;
- }
- Return $ hash % 701819;
- }
- Function APHash ($ str) // 0.30
- {
- $ Hash = 0;
- $ N = strlen ($ str );
- For ($ I = 0; $ I <$ n; $ I ++)
- {
- If ($ I & 1) = 0)
- {
- $ Hash ^ = ($ hash <7) ^ ord ($ str [$ I]) ^ ($ hash> 3 ));
- }
- Else
- {
- $ Hash ^ = (~ ($ Hash <11) ^ ord ($ str [$ I]) ^ ($ hash> 5 )));
- }
- }
- Return $ hash % 701819;
- }
- Function DEKHash ($ str) // 0.23
- {
- $ N = strlen ($ str );
- $ Hash = $ n;
- For ($ I = 0; $ I <$ n; $ I ++)
- {
- $ Hash = ($ hash <5) ^ ($ hash> 27) ^ ord ($ str [$ I]);
- }
- Return $ hash % 701819;
- }
- Function FNVHash ($ str) // 0.31
- {
- $ Hash = 0;
- $ N = strlen ($ str );
- For ($ I = 0; $ I <$ n; $ I ++)
- {
- $ Hash * = 0x811C9DC5;
- $ Hash ^ = ord ($ str [$ I]);
- }
- Return $ hash % 701819;
- }
- Function PJWHash ($ str) // 0.33
- {
- $ Hash = $ test = 0;
- $ N = strlen ($ str );
- For ($ I = 0; $ I <$ n; $ I ++)
- {
- $ Hash = ($ hash <4) + ord ($ str [$ I]);
- If ($ test = $ hash &-268435456 )! = 0)
- {
- $ Hash = ($ hash ^ ($ test> 24 ))&(~ -268435456 ));
- }
- }
- Return $ hash % 701819;
- }
- Function PHPHash ($ str) // 0.34
- {
- $ Hash = 0;
- $ N = strlen ($ str );
- For ($ I = 0; $ I <$ n; $ I ++)
- {
- $ Hash = ($ hash <4) + ord ($ str [$ I]);
- If ($ g = ($ hash & 0xF0000000 )))
- {
- $ Hash = $ hash ^ ($ g> 24 );
- $ Hash = $ hash ^ $ g;
- }
- }
- Return $ hash % 701819;
- }
- Function OpenSSLHash ($ str) // 0.22
- {
- $ Hash = 0;
- $ N = strlen ($ str );
- For ($ I = 0; $ I <$ n; $ I ++)
- {
- $ Hash ^ = (ord ($ str [$ I]) <($ I & 0x0f ));
- }
- Return $ hash % 701819;
- }
- Function MD5Hash ($ str) // 0.050
- {
- $ Hash = md5 ($ str );
- $ Hash = $ hash [0] | ($ hash [1] <8) | ($ hash [2] <16) | ($ hash [3] <24) | ($ hash [4] <32) | ($ hash [5] <40) | ($ hash [6] <48) | ($ hash [7] <56 );
- Return $ hash % 701819;
- }
Algorithm description: in the comments following the function, the speed of executing the local test is 1000 times (unit: s). It can be seen that MD5Hash is the fastest, and it is much faster than other functions... however, we can see from the algorithm of this function that it only depends on the first seven characters of the md5 string, that is to say, if the first seven characters are the same, the obtained hash values are exactly the same. Therefore, the distribution of hash values is not very trustworthy .... if the computation speed is 32 characters, it will be much slower than other algorithms...
In addition to MD5Hash, other algorithms will be affected by the length of the string. the longer the algorithm is, the slower it is. I tested the algorithm in English with 10 characters. The return $ hash % 701819 at the end of each function; 701819 indicates the maximum volume of hash, that is, the range of the numbers obtained by these hash functions is 0 ~ 701819. this number can be changed. it is generally considered that the distribution of a large prime number result will be relatively uniform. the recommended values in the vicinity of 701819 are: 175447,350 899, 1403641,280 7303, 5614657.
What can this be used...
Why are we organizing and testing these hash algorithms? I'm writing a multi-user Blog, well... as mentioned in the previous log, a multi-user Blog generally has a function that uses a username combined with English and numbers as the Blog address (second-level domain name or directory ). Then there is a problem. how can I obtain the user ID based on the user name and perform another query? With the hash function, you don't need to use the hash function to process the user name, get a number, and then perform some processing on the number (I divide the number into two levels of directories, the purpose is to prevent the disk retrieval speed from being affected by too many files in a directory.) then, a path is formed, store the corresponding ID in the file under this path (I personally recommend the user name as the file name), so that you can directly obtain the user ID based on the user name, without querying, the user name is used as the file name, so even if the final result is the same, it is in different files, so you don't have to worry about collision.
Of course... if your system is operating based entirely on the user name, when I did not say = B before, then a private comment on the SELECT statement is also a number that is faster than the string.
I chose the DJB algorithm. after going online, if the MD5 distribution test is acceptable, I will try again.
It can also be seen from this that hash is still very useful for distribution. it can be used for caching, static or other things that require distributed storage. |