The "standard" way to compute the longest left-anchored common substring
would be to compare character for character of both strings until they differ.
However, in Perl it's relatively cumbersome (compared to C for example)
to access a single character in a string (via the substr
builtin), so this approach is quite slow (even for rather short strings like your sample).
For comparison, here's the results of a quick benchmark, so you'll actually
appreciate the performance of the XOR method suggested by Corion:
#!/usr/bin/perl
use Benchmark qw(cmpthese);
use Inline C;
my $s1 = '/home/usernames/doejohnwilson';
my $s2 = '/home/usernames/doejanemary';
cmpthese(-1, {
'standard' => sub {
my $pos = 0;
$pos++ while substr($s1, $pos, 1) eq substr($s2, $pos, 1);
my $result = substr($s1, 0, $pos);
# die unless $result eq "/home/usernames/doej";
},
'fancy-XOR' => sub {
my $common = $s1 ^ $s2;
my $common_length = 0;
if ($common =~ /^(\x00+)/) {
$common_length = length $1;
};
my $result = substr($s1, 0, $common_length);
# die unless $result eq "/home/usernames/doej";
},
'standard-XS' => sub {
my $common_length = left_anchored_common_substr_len($s1, $s2);
my $result = substr($s1, 0, $common_length);
# die unless $result eq "/home/usernames/doej";
}
});
__END__
__C__
int left_anchored_common_substr_len(char* s1, char* s2) {
int c = 0;
while (*s1++ == *s2++) { c++; }
return c;
}
Results:
Rate standard fancy-XOR standard-XS
standard 109227/s -- -77% -92%
fancy-XOR 468114/s 329% -- -64%
standard-XS 1291788/s 1083% 176% --
Update: added an XS routine (via Inline::C) that implements the aforementioned "standard" approach in C. |