Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by Anonymous Monk
on Jul 09, 2011 at 05:06 UTC ( [id://913485]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Hamming Distance Between 2 Strings - Fast(est) Way?
in thread Hamming Distance Between 2 Strings - Fast(est) Way?

Here is something along the lines of what monkfan had probably written
#!/usr/bin/perl -- use strict; use warnings; use Benchmark qw(cmpthese); our $data; print "\n$]\n"; { my $s1 = 'AAAAA'; my $s2 = 'ATCAA'; print join ' ', RoyJohnson500332($s1,$s2)," "; print join ' ', monkfan500235($s1,$s2)," "; print join ' ', BrowserUk500244($s1,$s2)," "; print join ' ', inman500994($s1,$s2)," "; print "\n"; } for my $range ( 5, 1_000 , 2_000 , 10_000 , 100_000 ){ $data = join '',map { ( qw' T A C G ' )[ $_ % 4 ] } 0 .. $range; my $s1 = $data.'AAAAA'; my $s2 = $data.'ATCAA'; print "## Length $range ", "##" x 11, "\n"; cmpthese (-3, { Mine => sub { monkfan500235($s1,$s2); return }, BUk => sub { BrowserUk500244($s1,$s2); return }, inman => sub { inman500994($s1,$s2); return }, RJ => sub { RoyJohnson500332($s1,$s2); return }, }); print "\n"; } sub monkfan500235 { 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; } sub BrowserUk500244 { length( $_[ 0 ] ) - ( ( $_[ 0 ] ^ $_[ 1 ] ) =~ t +r[\0][\0] ) } sub RoyJohnson500332 { my ($k, $l) = @_; my $diff = $k ^ $l; my $num_mismatch = $diff =~ tr/\0//c; } sub inman500994 { return ($_[0] ^ $_[1]) =~ tr/\001-\255//; } __END__ 5.012002 2 2 2 2 ## Length 5 ###################### Rate Mine RJ BUk inman Mine 127809/s -- -78% -85% -87% RJ 580454/s 354% -- -33% -43% BUk 872298/s 582% 50% -- -14% inman 1012310/s 692% 74% 16% -- ## Length 1000 ###################### Rate Mine RJ BUk inman Mine 1862/s -- -99% -99% -99% RJ 176683/s 9389% -- -18% -21% BUk 216727/s 11540% 23% -- -4% inman 224899/s 11979% 27% 4% -- ## Length 2000 ###################### Rate Mine RJ BUk inman Mine 934/s -- -99% -99% -99% RJ 100661/s 10677% -- -17% -19% BUk 120656/s 12818% 20% -- -2% inman 123544/s 13127% 23% 2% -- ## Length 10000 ###################### Rate Mine RJ inman BUk Mine 187/s -- -99% -99% -99% RJ 22959/s 12181% -- -16% -18% inman 27435/s 14575% 19% -- -2% BUk 27976/s 14865% 22% 2% -- ## Length 100000 ###################### Rate Mine RJ inman BUk Mine 18.7/s -- -99% -99% -99% RJ 2085/s 11031% -- -21% -22% inman 2651/s 14054% 27% -- -1% BUk 2667/s 14136% 28% 1% --

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-25 08:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found