http://www.perlmonks.org?node_id=500235

monkfan has asked for the wisdom of the Perl Monks concerning the following question:

My most revered monks,

The subroutine below compute the number of mismatches between two strings (they are always equal) - usually called Hamming Distance.

However my code below is painfully slow. I am terribly in need of a super fast way to do this. In particular it has to process millions of string pairs.

Thus I turn to you my brother monks, for illumination in this matter.
#!/usr/bin/perl -w use strict; my $s1 = 'AAAAA'; my $s2 = 'ATCAA'; my $s3 = 'AAAAA'; hd($s1,s2) # will give value 2 hd($s1,s3) # will give value 0 sub hd { #String length is assumed to be equal my ($k,$l) = @_; my $len = length ($k); my $num_mismatch = 0; for (my $i=0; $i<$len; $i++) { ++$num_mismatch if substr($k, $i, 1) ne substr($l, $i, 1); } return $num_mismatch; }
Update: Benchmark. Thanks so much everybody.
Rate Mine RJ BWK inman Mine 176987/s -- -65% -78% -80% RJ 504123/s 185% -- -38% -44% BWK 817943/s 362% 62% -- -9% inman 901871/s 410% 79% 10% -- *Note: Mine has already included the modification suggested by 'wazoox'.

Regards,
Edward