http://www.perlmonks.org?node_id=1037346


in reply to Re^4: how to unpack a C struct with length array preceding data array
in thread how to unpack a C struct with length array preceding data array

This is what I mean by "not really extensible":

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; our $I //= -1; our $N //= 4; my @ls = map int( 2+rand 10 ), 1 .. $N; our $str = pack 'S*', @ls; $str .= 'x' x ($_-1) . ' ' for @ls; cmpthese $I, { a => q[ # line 1 "a" my $templ = "S$N" . join( ' ', map{ "\@0 x[S$_] S \@0" . "S$_ x[S${ \( $N-$_ )}]" . '/x' x $_ . '/a' } 0 .. $N-1 ); my( @d ) = unpack $templ, $str; shift @d for 1 .. $N; $I == 1 and print 'a ', join '|', @d; ], b => q[ # line 1 "b" my @l = unpack "S$N", $str; my $templ = "x[S$N]" . join( ' a', '', @l ); my @d = unpack $templ, $str; $I == 1 and print 'b ', join '|', @d; ], }; __END__ C:\test>junk -I=-1 -N=1 Rate a b a 128551/s -- -59% b 312873/s 143% -- C:\test>junk -I=-1 -N=10 Rate a b a 19086/s -- -67% b 57153/s 199% -- C:\test>junk -I=-1 -N=100 Rate a b a 894/s -- -89% b 7979/s 793% --

You can clean that up by not returning the counts -- which are redundant -- but it is still doing a shit load of extra work in order to avoid two calls to unpack.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.