Tell us about the larger picture. There is likely to be room for improving whatever you are doing at present, or at least for us to target solutions to whatever the bigger problem is. Most gains in performance are not obtained by micro-optimizations such as you are asking for. Some tricks that will give huge performance gains in some situations will cripple the code in other situations.
That said, you can build masks then xor the strings to give something like the fast match you are looking for where the strings to be matched are fairly long. Consider:
use strict;
use warnings;
my $strA = 'ATGNCNC';
my $strB = 'ATGACNN';
my $strC = 'TTGACNN';
print $strA, (match ($strA, $strB) ? ' eq' : ' ne'), " $strB\n";
print $strA, (match ($strA, $strC) ? ' eq' : ' ne'), " $strC\n";
sub match {
my ($mask1, $mask2) = @_;
my ($str1, $str2) = @_;
$mask1 =~ tr/NATGC/0\xFF/;
$mask2 =~ tr/NATGC/0\xFF/;
$mask1 &= $mask2;
$str1 ^= $str2;
$str1 &= $mask1;
return $str1 !~ /[^\x00]/;
}
Prints:
ATGNCNC eq ATGACNN
ATGNCNC ne TTGACNN
If you can cache the masks (say you were matching all strings against all others for example) then you get a greater gain.
True laziness is hard work
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.