use strict;
use Benchmark qw(timethese);
# Input data
my @fields = (
[ name1 => { start => 0, len => 10 } ],
[ name2 => { start => 8, len => 3 } ],
[ name3 => { start => 11, len => 14 } ],
);
# This was the first idea I had.
my $pos = 0;
my $template_1;
for my $field (@fields) {
my $mismatch = $field->[1]{start} - $pos;
if ($mismatch > 0) {
$template_1 .= "x$mismatch";
} elsif ($mismatch < 0) {
$mismatch = 0 - $mismatch;
$template_1 .= "X$mismatch";
}
$template_1 .= "A" . $field->[1]{len};
$pos = $field->[1]{start} + $field->[1]{len};
}
# This was the idea I had very shortly after, while looking at -f pack
my $template_2 = join '', map { "\@$_->[1]{start}A$_->[1]{len}" } @fie
+lds;
# Benchmark it
print "t1: $template_1\nt2: $template_2\n";
my $str = "0123456789abcdefghijklmnopqrstuvwxyz";
timethese( 3_000_000, {
t1 => sub {
my @a = unpack ($template_1, $str)
},
t2 => sub {
my @a = unpack ($template_2, $str)
}
}
);
# Templates and Benchmark results
# t1: A10X2A3A14
# t2: @0A10@8A3@11A14
# Benchmark: timing 3000000 iterations of t1, t2...
# t1: 8 wallclock secs ( 8.82 usr + 0.02 sys = 8.84 CPU) @ 339366.5
+2/s (n=3000000)
# t2: 9 wallclock secs ( 9.46 usr + 0.00 sys = 9.46 CPU) @ 317124.7
+4/s (n=3000000)
-
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.
|