Not really. Did I claim it was optimised! You wanted to use substring so I put it in. It works a lot better without it unpacking the ints straight into a list to iterate over rather than using substr.

BTW print is very slow so never benchmark with debugging prints in action as your results will be meaningless. Try this (unpacking is now 4 times as fast as packing). Uncomment the commented out prints if you want to make sure it is still working (and the $DEBUG). print "blah" if $DEBUG works fine for testing but that is a helluva lot of ifs you put into your benchmark if you don't comment out the prints.

my $MSB = 1<<31; my $SET_MSB = pack "I",$MSB; my $UNSET_MSB = pack "I",$MSB-1; my $tests = 250_000; # my $DEBUG = 1; print "Doing $tests tests\n"; my $start = time(); my $str = ''; for my $doc_id( 1 .. $tests ) { #printf "Doc id: %d\n", $doc_id if $DEBUG; $str .= (pack "I", $doc_id) | $SET_MSB; for my $pos ( 0 .. 2 ) { $str .= (pack "I", $pos) & $UNSET_MSB; } } printf "pack time %d\n", time()-$start; printf "length %d\n", length $str; my $unpack = time(); my $dat = {}; my $doc_id = undef; for my $int (unpack "I*", $str) { if ( $int > $MSB ) { $doc_id = unpack "I", ((pack "I", $int) & $UNSET_MSB); #printf "\nDoc id: %d\t", $doc_id if $DEBUG; } else { push @{$dat->{$doc_id}}, $int; #print "$int\t" if $DEBUG; } } printf "unpack time %d\n",time()-$unpack; __DATA__ C:\>serialize.pl Doing 250000 tests pack time 4 length 4000000 unpack time 1 C:\>

In reply to Re^5: Byte allign compression in Perl.. by tachyon-II
in thread Byte allign compression in Perl.. by MimisIVI

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.