Yes, 2 elements 50 times.

#!perl use 5.006; # file: pack.t use strict; use warnings; use Benchmark qw(:all) ; use Data::Dumper; use List::Util qw(pairs); my @diffs = map { $_,$_; } (0..49); #print '@diffs: ',Dumper(\@diffs),"\n"; my $packed0 = pack('V*',@diffs); my $a = []; #print 'len: ',length( $packed0),"\n"; my $packed = $packed0; while (length( $packed)) { push @$a,[unpack ('VV', substr( $packed, 0, 8, ''))]; } #print Dumper($a); timethese( 50_000, { 'unpack while' => sub { my $packed = $packed0; while (length( $packed)) { my ($x,$y)= unpack ('VV', substr( $packed, 0, 8, '')); } }, 'unpack while single' => sub { my $packed = $packed0; while (length( $packed)) { my $x = unpack ('V', substr( $packed, 0, 4, '')); } }, 'unpack while push' => sub { my $packed = $packed0; $a = []; while (length( $packed)) { push @$a,[unpack ('VV', substr( $packed, 0, 8, ''))]; } }, 'unpack for' => sub { for ( my $i = 0;$i < length( $packed0)-1; $i += 8 ) { my ($x,$y)= unpack ('VV', substr( $packed0, $i, 8)); } }, 'unpack for push' => sub { $a = []; for ( my $i = 0;$i < length( $packed0)-1; $i += 8 ) { push @$a,[unpack ('VV', substr( $packed0, $i, 8))]; } }, }); ######## $ perl pack.t Benchmark: timing 50000 iterations of unpack for, unpack for push, unp +ack while, unpack while push, unpack while single... unpack for: 1 wallclock secs ( 1.01 usr + 0.00 sys = 1.01 CPU) @ 49 +504.95/s (n=50000) unpack for push: 2 wallclock secs ( 1.84 usr + 0.00 sys = 1.84 CPU) + @ 27173.91/s (n=50000) unpack while: 1 wallclock secs ( 0.84 usr + 0.00 sys = 0.84 CPU) @ +59523.81/s (n=50000) unpack while push: 2 wallclock secs ( 1.75 usr + 0.00 sys = 1.75 CP +U) @ 28571.43/s (n=50000) unpack while single: 1 wallclock secs ( 1.28 usr + 0.00 sys = 1.28 +CPU) @ 39062.50/s (n=50000)

With bitmaps it would be an array of 2 scalars (2 x 64-bit IVs, 53 bits used in the original test case).


In reply to Re^4: Faster creation of Arrays in XS? by wollmers
in thread Faster creation of Arrays in XS? by wollmers

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.