Never under estimate the performance of the regex engine.
#! perl -slw
use strict;
use List::Util qw[ reduce ];
use Benchmark qw[ cmpthese ];
our $str;
sub TedP {
my ($c, $maxn, $n, $maxc) = ('', 0);
for (0..(length($str)-1)) {
$_ = substr($str, $_, 1);
if ($_ ne $c) {
$n = 1;
$c = $_;
}
else {
$n++;
if ($n > $maxn) {
$maxn = $n;
$maxc = $c;
}
}
}
return $maxc x $maxn;
}
sub regex {
return reduce{
length $a > length $b ? $a : $b
} $str =~ m[((.)\2+)]g;
}
for my $n ( 1 .. 6 ) {
$str = join'', map{chr(65+rand(26)) x int(rand 20) } 1 .. 10**$n;
print "\nString length ", length $str;
# print regex();
# print TedP();
cmpthese -1, { TedP=> \&TedP, Regex=> \®ex };
}
__END__
C:\test>551038
String length 89
Rate TedP Regex
TedP 12429/s -- -50%
Regex 24837/s 100% --
String length 939
Rate TedP Regex
TedP 1265/s -- -51%
Regex 2598/s 105% --
String length 9741
Rate TedP Regex
TedP 126/s -- -48%
Regex 242/s 92% --
String length 94791
Rate TedP Regex
TedP 12.6/s -- -43%
Regex 22.3/s 77% --
String length 949396
(warning: too few iterations for a reliable count)
(warning: too few iterations for a reliable count)
Rate TedP Regex
TedP 1.29/s -- -40%
Regex 2.16/s 67% --
String length 9496562
(warning: too few iterations for a reliable count)
(warning: too few iterations for a reliable count)
s/iter TedP Regex
TedP 7.72 -- -39%
Regex 4.74 63% --
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|