I need an algorithm to hash a st开发者_如何学编程ring of arbitrary length into a value from 1 through 26.
I realize that I could sum the values of the individual characters and do a mod 26 + 1, but thought maybe someone would have a more performant way of doing this.
Do you need to make hash values not to change across different runs of an app? If you don't, and you want to use it in memory, simply use the system provided GetHashCode
function:
(s.GetHashCode() % 26) + 1
will map a string to an integer between 1 and 26 inclusive. Of course, this is not really suitable for tasks like persisting in a database.
Modulus and increment operators are very efficient. This will probably work fine for almost all purposes.
Have you identified a specific performance issue? If so, what are the circumstances?
Using the hash code as the other poster suggested as the basis for the modulus will give you a statistically more even spread of values compared to summing the ASCII/Unicode values of individual characters. The hash operation is quite fast as well, though probably a tiny bit slower than summing values of each position.
The solution you've suggested seems fine, as long as the method you use matches strings to hashes evenly and a small change in the string should create a fairly big change in the hash.
精彩评论