What is the best hash function for Rabin-Karp algorithm?
A extremly good performing hash is the bernstein hash. It even outruns many popular hashing algorithms.
unsigned bernstein_hash ( void *key, int len )
{
unsigned char *p = key;
unsigned h = 0;
int i;
for ( i = 0; i < len; i++ )
h = 33 * h + p[i];
return h;
}
Of course, you can try out other hashing algorithms, as described here: Hash function on NIST
Note: It has never been explained why the 33
is performing so much better
than any other "more logic" constant.
For your interest: Here is a good comparison of different hash algorithms: strchr comparison of hash algorithms