Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Hamming Distance Between 2 Strings - Fast(est) Way?

by wazoox (Prior)
on Oct 14, 2005 at 15:34 UTC ( #500263=note: print w/replies, xml ) Need Help??


in reply to Hamming Distance Between 2 Strings - Fast(est) Way?

Depending upon your data, simply checking first that the strings don't match can make it much faster :

sub hd1 { #String length is assumed to be equal my ($k,$l) = @_; # next line added return 0 if $k eq $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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://500263]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2020-11-26 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?