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}" } @fields; # 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.52/s (n=3000000) # t2: 9 wallclock secs ( 9.46 usr + 0.00 sys = 9.46 CPU) @ 317124.74/s (n=3000000)