I'm learning C right now and I had originally built this hash function for a spell checker program I'm building in a CS50 edx course.
int hashing(char *word)
{
unsigned int hash = 0;
for (int i = 0, n = strlen(word); i < n; i++)
hash += word[i];
return hash % HTABLE_SIZE;
}
Then I stumbled upon this hash function on reddit that uses bit-shift operators.
int hashing(char *word)
{
unsigned int hash = 0;
for (int i = 0, n = strlen(word); i < n; i++)
hash = (hash << 2) ^ word[i];
return hash % HTABLE_SIZE;
}
With this hash function the speed of my program went from 0.13 seconds to 0.06 seconds. Can someone please explain to me why this hash function is so much faster?