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)

In reply to Re: Challenge: Construct an unpack string by hobbs
in thread Challenge: Construct an unpack string by holli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.