Created
June 14, 2021 14:25
-
-
Save trwyant/4b308aed5b220f3f810af933aeb0790b to your computer and use it in GitHub Desktop.
Perl regular expression timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use 5.010; | |
use strict; | |
use warnings; | |
use Benchmark; | |
use Getopt::Long 2.33 qw{ :config auto_version }; | |
use Pod::Usage; | |
our $VERSION = '0.000_111'; | |
my %opt = ( | |
count => 100_000_000, | |
match => 'fubar', | |
); | |
GetOptions( \%opt, | |
qw{ count|iterations=i match_string|match-string=s }, | |
help => sub { pod2usage( { -verbose => 2 } ) }, | |
) or pod2usage( { -verbose => 0 } ); | |
@ARGV | |
or @ARGV = ( | |
'm/ \A /msx', | |
'm/ ^ /msx', | |
'm/ .? /msx', | |
'm/ .{0} /msx', | |
'm/ (*ACCEPT) /msx', | |
'm/ (?:) /msx', | |
'm/ (?) /msx', | |
'm/ /msx', | |
'qr/ \A /msx', | |
'qr/ ^ /msx', | |
'qr/ .? /msx', | |
'qr/ .{0} /msx', | |
'qr/ (*ACCEPT) /msx', | |
'qr/ (?:) /msx', | |
'qr/ (?) /msx', | |
'qr/ /msx', | |
); | |
my $match_string = $opt{match}; | |
timethese( | |
$opt{count}, | |
make_matchers( @ARGV ), | |
); | |
sub make_matchers { | |
my @arg = @_; | |
my %rslt; | |
foreach my $match ( @arg ) { | |
$rslt{$match} = eval "sub { \$match_string =~ $match }"; | |
} | |
return \%rslt; | |
} | |
__END__ | |
=head1 TITLE | |
match-nothing - Benchmark various ways of matching nothing | |
=head1 SYNOPSIS | |
match-nothing | |
match-nothing 'm/\A/' | |
match-nothing --help | |
match-nothing --version | |
=head1 OPTIONS | |
=head2 --count | |
--count=1000000000 | |
This option specifies the count given to C<timethese()>. | |
The default is C<--count=100000000> (i.e. C<10 ** 8>). | |
=head2 --help | |
This option displays the documentation for this script. The script then | |
exits. | |
=head2 --match-string | |
--match-string='Able was I ere I saw Elba' | |
This option specifies the string against which the match is performed. | |
The default is C<--match-string=fubar>. | |
=head2 --version | |
This option displays the version of this script. The script then exits. | |
=head1 DETAILS | |
This Perl script uses the L<Benchmark|Benchmark> module to compare the | |
performance of various regular expresions. Any regular expression can be | |
specified on the command line; the name of the script comes from its | |
creation as a way to investigate the most efficient regular expression | |
to match any string successfully. | |
=head1 AUTHOR | |
Thomas R. Wyant, III F<wyant at cpan dot org> | |
=head1 COPYRIGHT AND LICENSE | |
Copyright (C) 2021 by Thomas R. Wyant, III | |
This program is free software; you can redistribute it and/or modify it | |
under the same terms as Perl 5.10.0. For more details, see the full text | |
of the licenses in the directory LICENSES. | |
This program is distributed in the hope that it will be useful, but | |
without any warranty; without even the implied warranty of | |
merchantability or fitness for a particular purpose. | |
=cut | |
# ex: set textwidth=72 : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment