Tag: 汉明距离

寻找最快的汉明距离C实现

我想找到两个等长的字符串有多少个不同的字符。 我发现xoring算法被认为是最快的,但它们返回以位表示的距离。 我希望结果以字符表示。 假设“pet”和“pit”的距离1以字符表示,但“e”和“i”可能有两个不同的位,因此xoring返回2。 我写的函数是: // 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; } 会变得更快吗? 也许使用一些较低级别的命令或实现不同的算法? 系统:Intel Xeon X5650上的Gcc 4.7.2 谢谢