Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by Roy Johnson (Monsignor)
on Oct 14, 2005 at 17:52 UTC ( #500332=note: print w/replies, xml ) Need Help??


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

Or a little more plainly (update to explain: using the names the OP did, so he can see what corresponds):
sub hd { my ($k, $l) = @_; my $diff = $k ^ $l; my $num_mismatch = $diff =~ tr/\0//c; }

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^3:Hamming Distance Between 2 Strings - Fast(est) Way?
by BrowserUk (Pope) on Oct 14, 2005 at 19:06 UTC

    I'm not sure that $k, $l are any more meaningful than $_[0], $_[1] and you pay a small performance penalty for obtaining those names.

    Does $diff really capture what it is naming? I've used $xor or $mask for that in the past, but I wonder if it isn't best left unnamed.

    I don't see any purpose in naming the return value, just to return it. Better to say return <EXPR>; and give the function a proper name like hammingDistance() and allow it to name the return value.

    While using tr/\0//c produces the same result as length - tr/\0/\0/, the major difference is that the former modifies the string being inspected, deleting the chars counted, where the latter does not. It just counts.

    On the short strings in the OP this is insignificant. But the technique works for any length strings, and as DNA strings can get very large indeed, the difference then becomes very significant.

    Overall, I prefer my version to yours, but each to their preference :)


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      If I were writing it from scratch, I'd name things differently. I used the names the OP did, so that he could see the correspondence. I added a parenthetical explanation to my original post to make that clear.

      I wasn't going to make a new reply, but I had to correct your mistaken notion that tr deletes things when it doesn't have the /d option appended.


      Caution: Contents may have been coded under pressure.

        Of course you are right, nothing is deleted without /d.

        However, the "count the stars" mode does seem to be consistantly around 10% quicker?

        $s = "\0\1" x 10000;; cmpthese -1, { A=> q[ $n1 = $s =~ tr[\0][\0] ], B=> q[ $n2 = $s =~ tr[\0][]c ] }; Rate B A B 23522/s -- -9% A 25935/s 10% -- print "$n1 : $n2";; 10000 : 10000

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2020-11-27 03:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?