You make a good point, but I found myself wondering how the combination of substr, length, and eq would compare to a single anchored regex, so I did a benchmark. I'm convinced!
bannor:~/work/perl/monks$ cat 968838.pl
#!/usr/bin/env perl
use Modern::Perl;
use Benchmark qw(:all);
my $str = 'A.SERIAL_NUMBER||abcdefghijklmnopqrstuvwxyz';
my $ss = 'A.SERIAL_NUMBER||';
my $yay;
cmpthese( 10_000_000, {
'substr' => sub {
if(substr($str,0,length($ss)) eq $ss ){
$yay = 1;
}
},
'regex' => sub {
if( $str =~ /^\Q$ss\E/ ){
$yay = 1;
}
},
});
bannor:~/work/perl/monks$ perl 968838.pl
Rate regex substr
regex 1312336/s -- -65%
substr 3802281/s 190% --
|