I want to find how many different characters two strings of equal length have. I have found that xoring algorithms are considered to be the fastest, but they return distance expressed in bits. I want the results expressed in characters. Suppose that "pet" and "pit" have distance 1 expressed in characters but 'e' and 'i' might have two different bits, so xoring returns 2.
The function i wrote is:
// na = length of both strings
unsigned int HammingDistance(const char* a, unsigned int na, const char* b) {
unsigned int num_mismatches = 0;
while (na) {
if (*a != *b)
++num_mismatches;
--na;
++a;
++b;
}
return num_mismatches;
}
Could it become any faster? Maybe using some lower level commands or implementing a different algorithm?
System: Gcc 4.7.2 on Intel Xeon X5650
Thank you
0
. However it might not optimal, if a shorter branchless instruction sequence can be devised - look up branchless condtionals or branchless code. – Doubling