Benchmarking Regular Expressions:
[1 2 3]
#!/usr/bin/perl
use strict;
use Benchmark;
use vars qw(@rules @lines $iters @compiles $tests);
@rules = (
'b R \bgsdegrjk\.com',
'b R (\.|\/|@)gsdegrjk\.com'
);
@lines = (
'TA0NJ6CtUsNHo/uwj2Ln93QLLwJxRgZ1VvwDCM7PBR4/DDDc8b1RiSJgkgbvPUBn5GXfIVdC7nNz',
'PgPKBaqhnphQRNUT2tsncamXK4Jggj9gKb80UtjGqMI9te4G6ARVU9HznPLTfILyyP+sjdfisBeG',
'IC3h9pVZ32Mr5L98c/UzNTagO8EeLj0fof8py2wyBoJAxtvCkV/7w98mpBFtR2Ylz/D5+9Dz2Ucs',
'Y1FkjiYnq6QojlgEjbFPj05AGtSTdTGLYLf+ji+TKfQv1IXsJlIH5sLAMz8KKWl1iarWu3a82MWs',
'IBbUv4oLdFbJ2JXt8NPYNrhHhfLkCLSUiyg9fJICZDFour/atgRktkw9WaW/RX6TDUuCl5GZlsTi'
);
$iters = 75000;
foreach my $rule (@rules) {
my $index = scalar(@compiles);
my ($ci,$score,$regex) = $rule =~ /^(.) (\-?\d+|A|R) (.*)$/;
die qq#Rule '$rule' is not formatted properly\n# unless $regex;
if ($ci eq 'H' || $ci eq 'B') { # case sensitive match
my $re = qr#$regex#; push(@compiles,[$re,$score,$regex,$ci]);
} elsif ($ci eq 'h' || $ci eq 'b') { # case insensitive match
my $re = qr#$regex#i; push(@compiles,[$re,$score,$regex,$ci]);
} else {
die qq#Rule '$rule' is not a header or body rule.\n#;
}
print $index+1,": '$rule'\n";
$tests->{"Rule ".($index+1)} = "\&RunMatch(\$compiles[$index])";
};
print qq#Testing each regex against each line:\n#;
foreach my $line (@lines) {
foreach my $rule (@compiles) {
($line =~ $rule->[0]) ? print "PASS\t" : print "FAIL\t";
}
print qq#$line\n#;
}
sub RunMatch { foreach my $line (@lines) { $line =~ $_[0]->[0]; } }
print qq#\nStarting iterations:\n#;
timethese($iters,$tests);
exit(0);