Various string hash function comparison and various hash algorithm code Daquan __div

Source: Internet
Author: User
Tags comparison hash key string

Commonly used string hash functions and Elfhash,aphash and so on, are very simple and effective methods. These functions use bitwise operations to make each character have an effect on the last function value. There are also hash functions represented by MD5 and SHA1, which are almost impossible to find collisions with.

Common string hash functions are bkdrhash,aphash,djbhash,jshash,rshash,sdbmhash,pjwhash,elfhash and so on. For the above several hash functions, I have a small evaluation of it.

hash function Data 1 Data 2 Data 3 Data 4 Data 1 Score Data 2 Score Data 3 Score Data 4 Score Average score
Bkdrhash 2 0 4774 481 96.55 100 90.95 82.05 92.64
Aphash 2 3 4754 493 96.55 88.46 100 51.28 86.28
Djbhash 2 2 4975 474 96.55 92.31 0 100 83.43
Jshash 1 4 4761 40W 100 84.62 96.83 17.95 81.94
Rshash 1 0 4861 505 100 100 51.58 20.51 75.96
Sdbmhash 3 2 4849 504 93.1 92.31 57.01 23.08 72.41
Pjwhash 30 26 4878 513 0 0 43.89 0 21.95
Elfhash 30 26 4878 513 0 0 43.89 0 21.95

The number of random string hash conflicts in which data 1 is 100,000 letters and numbers. Data 2 is the number of hash conflicts for 100,000 meaningful English sentences. The number of conflicts in the linear table is stored in data 3 after modulo the hash value of data 1 and 1000003 (large primes). Data 4 is the number of conflicts that are stored in a linear table after modulo the hash value of data 1 and 10000019 (larger primes).

After comparison, the above average score is obtained. The average is the square average. It can be found that the effect of Bkdrhash is the most outstanding in both the actual effect and the coding implementation. Aphash is also a more excellent algorithm. Djbhash,jshash,rshash and Sdbmhash. Pjwhash and Elfhash effect is the worst, but the score is similar, its algorithm essence is similar.

Appendix 1: C program code for various hash functions

#define M 249997 #define M1 1000001 #define M2 0xf0000000/RS Hash Function unsigned int rshash (CHAR*STR) { 
    unsigned int b=378551; 
    unsigned int a=63689;     
    unsigned int hash=0; 
        while (*STR) {hash=hash*a+ (*str++); 
    A*=b; 
Return (hash% M);    
    }//JS Hash Function unsigned int jshash (CHAR*STR) {unsigned int hash=1315423911; 
    while (*STR) {hash^= (hash<<5) + (*str++) + (hash>>2)); 
Return (hash% M); }//P. Weinberger Hash Function unsigned int pjwhash (CHAR*STR) {unsigned int bitsinunignedint= (unsigned in 
    T) (sizeof (unsigned int) *8); 
    unsigned int threequarters= (unsigned int) ((bitsinunignedint*3)/4); 
    unsigned int oneeighth= (unsigned int) (BITSINUNIGNEDINT/8); 
    unsigned int highbits= (unsigned int) (0xFFFFFFFF) << (bitsinunignedint-oneeighth); 
    unsigned int hash=0;     
    unsigned int test=0; while (*STR) {haSh= (hash<<oneeighth) + (*str++); 
        if ((test=hash&highbits)!=0) {hash= ((hash^ (Test>>threequarters)) & (~highbits)); 
} return (hash% M); 
    }//ELF Hash Function unsigned int elfhash (CHAR*STR) {unsigned int hash=0;    
    unsigned int x=0; 
        while (*STR) {hash= (hash<<4) + (*str++); 
            if ((x=hash&0xf0000000l)!=0) {hash^= (x>>24); 
        Hash&=~x; 
} return (hash% M); }//BKDR Hash Function unsigned int bkdrhash (CHAR*STR) {unsigned int seed=131;//131 1313 13131-131313 et     
    C.. unsigned int hash=0; 
    while (*STR) {hash=hash*seed+ (*str++); 
Return (hash% M);     
    }//SDBM Hash Function unsigned int sdbmhash (CHAR*STR) {unsigned int hash=0; 
    while (*STR) {hash= (*str++) + (hash<<6) + (hash<<16)-hash; RetUrn (hash% M);    
    }//DJB Hash Function unsigned int djbhash (CHAR*STR) {unsigned int hash=5381; 
    while (*STR) {hash+= (hash<<5) + (*str++); 
Return (hash% M); 
    }//AP Hash Function unsigned int aphash (CHAR*STR) {unsigned int hash=0;      
    int i; for (i=0;*str;i++) {if (i&1) ==0) {hash^= (hash<<7) ^ (*str++) ^ (hash>>3) 
        ); 
        else {hash^= ((hash<<11) ^ (*str++) ^ (hash>>5))); 
} return (hash% M); 
 }

Attach 2:hash algorithm encyclopedia

/** * Hash Algorithm <br> * Recommended use FNV1 algorithm * @algorithm None * @author goodzzp 2006-11-20 * @lastEdit GOODZZP 20   
    06-11-20 * @editDetail Create */public class Hashalgorithms {/** * add hash * @param key string
        * @param prime a prime number * @return Hash result */public static int Additivehash (String key, int prime) {
        int hash, I;
        for (hash = Key.length (), i = 0; i < key.length (); i++) hash = Key.charat (i);
    Return (hash% prime); /** * Rotary Hash * @param key input String * @param prime prime number * @return Hash value * * Public
        static int Rotatinghash (String key, int prime) {int hash, I; for (hash = Key.length (), i = 0; i < key.length (); ++i) hash = (hash << 4) ^ (hash >>) ^ key
        . charAt (i);
        Return (hash% prime);    
    Return (hash ^ (hash>>10) ^ (hash>>20)); ///substitution://use: hash =(hash ^ (hash>>10) ^ (hash>>20)) & mask;    

    Substitution: hash%= prime;
    /** * MASK value, just find a value, preferably prime number * * static int m_mask = 0X8765FED1; /** * One hash * @param key input String * @return Output hash value */public static int Onebyonehash (Stri
        ng key) {int hash, I;
            for (hash = 0, i = 0; i < key.length (); ++i) {hash + key.charat (i);
            Hash + = (hash << 10);
        Hash ^= (hash >> 6);
        hash = = (hash << 3);
        Hash ^= (hash >> 11);
        Hash + = (hash << 15);    
        Return (hash & m_mask);
    return hash;   
    /** * Bernstein ' s hash * @param key input byte array * @param level initial hash constant * @return result hash
        */public static int Bernstein (String key) {int hash = 0;
        int i;
   for (i = 0; i < key.length (); ++i) hash = * Hash + key.charat (i);     return hash;   }//////Pearson ' s Hash//char Pearson (Char[]key, Ub4 Len, Char tab[256])//{/    
    char Hash;    
    Ub4 i;    
    For (Hash=len, i=0; i<len; ++i)//hash=tab[hash^key[i]];    
    return (hash);    
    ////CRC hashing, compute CRC, specific code see other//Ub4 CRC (Char *key, Ub4 Len, Ub4 mask, ub4 tab[256])//{    
    UB4 hash, I;    
    For (Hash=len, i=0; i<len; ++i)//hash = (Hash >> 8) ^ tab[(hash & 0xff) ^ key[i]];    
    Return (hash & mask);
    }/** * Universal hashing/public static int universal (char[] key, int mask, int[] tab)
        {int hash = key.length, I, len = key.length;
            for (i = 0; i < (len << 3); i + = 8) {Char k = key[i >> 3];
            if ((k & 0x01) = = 0) hash ^= tab[i + 0]; if ((k & 0x02) = = 0) hash ^= tAb[i + 1];
            if ((k & 0x04) = = 0) hash ^= tab[i + 2];
            if ((k & 0x08) = = 0) hash ^= tab[i + 3];
            if ((k & 0x10) = = 0) hash ^= tab[i + 4];
            if ((k & 0x20) = = 0) hash ^= tab[i + 5];
            if ((k & 0x40) = = 0) Hash ^= tab[i + 6];
        if ((k & 0x80) = = 0) Hash ^= tab[i + 7];
    Return (hash & mask);
        }/** * Zobrist hashing/public static int Zobrist (char[] key, int mask, int[][] tab) {
        int hash, I;
        for (hash = key.length, i = 0; i < key.length; ++i) hash ^= tab[i][key[i]];
    Return (hash & mask);
    //LOOKUP3/See Bob Jenkins (3). c File//32-bit FNV algorithm static int m_shift = 0;    
    /** * 32-bit FNV algorithm * @param data array * @return int value */public static int Fnvhash (byte[] data)    
        {int hash = (int) 2166136261L;    
         for (byte b:data)   hash = (Hash * 16777619) ^ b;    
        if (M_shift = = 0) return hash;    
    Return (hash ^ (hash >> m_shift)) & M_mask; /** * Improved 32-bit FNV algorithm 1 * @param data array * @return int value */public static int Fnvhash    
        1 (byte[] data) {final int p = 16777619;    
        int hash = (int) 2166136261L;    
        for (byte b:data) hash = (hash ^ b) * p;    
        hash = = Hash << 13;    
        Hash ^= Hash >> 7;    
        hash = = Hash << 3;    
        Hash ^= Hash >> 17;    
        hash = = Hash << 5;    
    return hash; /** * Improved 32-bit FNV algorithm 1 * @param data String * @return int value */public static int Fnvhas    
        H1 (String data) {final int p = 16777619;    
        int hash = (int) 2166136261L; for (int i=0;i<data.length (); i++) hash = (hash ^ data.charat (i)) *P    
        hash = = Hash << 13;    
        Hash ^= Hash >> 7;    
        hash = = Hash << 3;    
        Hash ^= Hash >> 17;    
        hash = = Hash << 5;    
    return hash; /** * Thomas Wang's algorithm, integer hash */public static int inthash (int key) {key = ~ (k    
        EY << 15);    
        Key ^= (key >>> 10);    
        Key + + (key << 3);    
        Key ^= (key >>> 6);    
        Key + = ~ (key << 11);    
        Key ^= (key >>> 16);    
    Return key; /** * RS Algorithm hash * @param STR string */public static int Rshash (string str) {I
        NT B = 378551;
        int a = 63689;
        int hash = 0;
            for (int i = 0; i < str.length (); i++) {hash = hash * A + str.charat (i);
        A = a * b;
    Return (hash & 0x7fffffff);

    }/* End of RS Hash Function *//** * JS algorithm */public static int Jshash (String str) {int hash = 1315423911; for (int i = 0; i < str.length (); i++) {hash ^= (hash << 5) + Str.charat (i) + (hash >&gt ;
        2));
    Return (hash & 0x7fffffff);
        }/* End of JS Hash Function *//** * PJW algorithm * * public static int pjwhash (String str) {
        int bitsinunsignedint = 32;
        int threequarters = (Bitsinunsignedint * 3)/4;
        int oneeighth = BITSINUNSIGNEDINT/8;
        int highbits = 0xFFFFFFFF << (bitsinunsignedint-oneeighth);
        int hash = 0;
        int test = 0;

            for (int i = 0; i < str.length (); i++) {hash = (hash << oneeighth) + Str.charat (i); if ((test = hash & highbits)!= 0) {hash = ((hash ^ (test >> threequarters))
            & (~highbits)); } return (Hash & 0X7FFFFFFF);  }/* End of P. Weinberger Hash Function *//** * ELF algorithm */public static int Elfhash (String
        STR) {int hash = 0;
        int x = 0;
            for (int i = 0; i < str.length (); i++) {hash = (hash << 4) + Str.charat (i);
                if ((x = (int) (hash & 0xf0000000l))!= 0) {hash ^= (x >> 24);
            Hash &= ~x;
    } return (hash & 0x7fffffff); 
        }/* End of ELF Hash Function *//** * BKDR algorithm * * public static int bkdrhash (String str) { int seed = 131;    
        131 1313 13131 131313 etc..
        int hash = 0;
        for (int i = 0; i < str.length (); i++) {hash = (hash * seed) + Str.charat (i);
    Return (hash & 0x7fffffff); }/* End of BKDR Hash Function *//** * SDBM algorithm * * public static int Sdbmhash (STRing str) {int hash = 0; for (int i = 0; i < str.length (); i++) {hash = Str.charat (i) + (hash << 6) + (hash <<
        )-Hash;
    Return (hash & 0x7fffffff);
        }/* End of SDBM Hash Function *//** * DJB algorithm * * public static int djbhash (String str) {
        int hash = 5381;
        for (int i = 0; i < str.length (); i++) {hash = ((hash << 5) + hash) + Str.charat (i);
    Return (hash & 0x7fffffff);
        }/* End of DJB Hash Function *//** * DEK Algorithm * * public static int dekhash (String str) {
        int hash = Str.length (); for (int i = 0; i < str.length (); i++) {hash = ((hash << 5) ^ (hash >>)) ^ Str.char
        at (i);
    Return (hash & 0x7fffffff); }/* End of DEK Hash Function *//** * AP Algorithm */public static int APHaSH (String str) {int hash = 0; for (int i = 0; i < str.length (); i++) {Hash ^= ((i & 1) = 0)? ((hash << 7) ^ Str.charat (i) ^ (hash >> 3)): ((hash <<) ^ str
        . charAt (i) ^ (hash >> 5));    
        }//Return (hash & 0x7fffffff);
    return hash;
        }/* End of AP Hash Function *//** * java own algorithm * * public static int Java (String str) {
        int h = 0;
        int off = 0;
        int len = Str.length ();
        for (int i = 0; i < len; i++) {h = * * H + str.charat (off++);
    return h; /** * Mixed hash algorithm, output 64-bit value/public static long Mixhash (String str) {long hash = str.
        Hashcode ();
        Hash <<= 32;
        Hash |= FNVHash1 (str);
    return hash;
 }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.