Now I haven't benchmarked this assertion,
Why such a hurry?
but for now I'll risk injury and insult by taking the authors' word that unpack is faster.
Hold on. Here it comes. :)
It looks like substr is faster than unpack ...
#!/usr/bin/perl -w
use strict;
use Benchmark qw(timethese);
my $repeat = 10;
my $text = ('abc' x $repeat) . 'gotcha' . ('xyz' x $repeat);
my ($pre,$match,$post);
print "OS: $^O - Perl: $]\n";
timethese( 100000, {
'unpack' => sub {
if ($text =~ /gotcha/) {
$pre = prematch($text);
$post = postmatch($text);
$match = match($text);
}
},
'substr' => sub {
if ($text =~ /gotcha/) {
$pre = substr_prematch($text);
$post = substr_postmatch($text);
$match = substr_match($text);
}
},
}
);
if ($text =~ /gotcha/) {
print "unpack\n";
print "prematch :", prematch($text), "\n";
print "match :", match($text), "\n";
print "postmatch :", postmatch($text), "\n";
print "substring\n";
print "prematch :", substr_prematch($text), "\n";
print "match :", substr_match($text), "\n";
print "postmatch :", substr_postmatch($text), "\n";
}
sub prematch {
return unpack "a$-[0]", $_[0];
}
sub postmatch {
return unpack "x$+[0] a*", $_[0];
}
sub match {
my $len = $+[0] - $-[0];
unpack "x$-[0] a$len", $_[0];
}
sub substr_match {
substr( $_[0], $-[0], $+[0] - $-[0] )
}
sub substr_prematch {
substr( $_[0], 0, $-[0] )
}
sub substr_postmatch {
substr( $_[0], $+[0] )
}
__END__
(output edited to fit the page better)
OS: cygwin - Perl: 5.008
Benchmark: timing 100000 iterations of substr, unpack...
substr: 2 wallclock secs ( 1.92 usr + 0.00 sys = 1.92 CPU)
unpack: 11 wallclock secs (11.89 usr + 0.00 sys = 11.89 CPU)
unpack
prematch :abcabcabcabcabcabcabcabcabcabc
match :gotcha
postmatch :xyzxyzxyzxyzxyzxyzxyzxyzxyzxyz
substring
prematch :abcabcabcabcabcabcabcabcabcabc
match :gotcha
postmatch :xyzxyzxyzxyzxyzxyzxyzxyzxyzxyz
OS: MSWin32 - Perl: 5.006001
Benchmark: timing 100000 iterations of substr, unpack...
substr: 3 wallclock secs ( 1.93 usr + 0.00 sys = 1.93 CPU)
unpack: 4 wallclock secs ( 3.87 usr + 0.00 sys = 3.87 CPU)
unpack
OS: linux - Perl: 5.006001
Benchmark: timing 100000 iterations of substr, unpack...
substr: 2 wallclock secs ( 2.26 usr + 0.00 sys = 2.26 CPU)
unpack: 4 wallclock secs ( 3.90 usr + 0.00 sys = 3.90 CPU)
unpack
Update (1) And the reason is the overhead of interpolating $-[0] and $+[0] in the unpack parameter. If you run the benchmark with unpack "a30" and unpack "a36" it will be faster than substr but useless in general. So The Perl Cookbook was right after all. However, the devil is in the detail ... ;)
Update (2) The turning point is when the string is at least 5000 30,000 chars. With large strings, unpack becomes faster as advertised (Perl 5.6.1). Set $repeat to 5000, put an exit after timethese, and see the results. (Actually it is 5,000 groups of characters, thus creating a 30,000 chars string.)
Update (3) The original subs can be made slightly
faster using sprintf instead of a simple interpolation. Using this version, the turning point, where the unpack version becomes faster than the substr implementation, is down to 3500 chars groups (= 21,000 chars).
sub prematch {
unpack sprintf("a%d",$-[0]), $_[0];
}
sub postmatch {
unpack sprintf( "x%d a*", $+[0]) , $_[0];
}
sub match {
unpack sprintf ("x%d a%d", $-[0], $+[0] - $-[0] ), $_[0];
}
-
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.