Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Challenge: Construct an unpack string

by hobbs (Monk)
on Sep 27, 2006 at 21:02 UTC ( #575251=note: print w/replies, xml ) Need Help??


in reply to Challenge: Construct an unpack string

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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://575251]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2022-08-07 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?