I was curious about the efficiency of the generated regular expression from my previous post. I run some benchmarks, and the results are somewhat unexpected, at least for me!:
my $len = 300;
my @lines;
push @lines, join('', (map { 'f' . ('o' x rand $len * 1.5), (rand > .8
+ ? '. ' : ' ') } 0..rand 20 ), "\n")
for 0 .. 1000;
sub make_re {
my $len = shift;
my $re = "\\b"
. join('|', map("\\w(?:\\s[\\s\\w]{$_,}?", reverse 1 .. (
+$len - 3)), "\\w+\\s+")
. (")" x ($len - 3))
. "\\w+";
qr/($re)/;
}
# match maximal length sentence
my $len_minus_two = $len - 2;
sub max { my @m = grep /\s*\b\w(?=\w*\s+\w+)[\w\s]{$len_minus_two,}\w/
+o, @lines }
# match minimal length sentence
my $re = make_re $len;
sub min { my @m = grep /$re/, @lines }
use Benchmark qw(cmpthese);
cmpthese(-1, { max => \&max,
min => \&min } );
__OUTPUT__
Rate max min
max 24.3/s -- -26%
min 33.0/s 36% --
Note that the two regexps used match different things.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.