I am writing a script to read and decode binary files which contain numeric data. The file contains some 32-bit words which contain "Discrete" data(data fields less than 8-bits).

I have come up with a solution (see below), however it runs very slow since I must call this subroutine about 100 times per record. It takes about 1-minute to read a 4MB file, which contains about 4000 records. When I remove the calls to this subroutine, the time drops to about 17-seconds.

I am looking for suggestions on more efficient ways to do this. Any input would be appreciated. (There is a readmore section to describe the code displayed)
sub ReadBits { my $BitStringIn = @_[0]; my $OffsetRef = @_[1]; my $Length = @_[2]; my $ItemListRef = @_[3]; my $BitStringOut = 0; #capture the relevant bits $BitStringOut = substr($BitStringIn,$$OffsetRef,$Length); #update Offset $$OffsetRef += $Length; #determine the number of zero's I need to add to make a 32-bit num +ber $NumZeros = 32-$Length; #Create string of zeros with a length of $NumZeros $Zeros = unpack("B$NumZeros",pack("N",0)); #Insert string of zeros to $BitString to create a 32-bit string $BitStringOut = $Zeros.$BitStringOut; #Convert 32-bit string into a number $NumOut = unpack("N",pack("B32",$BitStringOut)); #Update ItemList @$ItemListRef = (@$ItemListRef,$NumOut); return($NumOut); }
$BitStringIn is the 32-bit number which contains the data I'm interested in, represented as a string of 1's and 0's.

$OffsetRef is a reference to the offset (in bits) of the data I'm interested in.

$Length is the length (in bits) of the data I'm interested in.

$ItemListRef is a reference to a list where I'm collecting data from the file.

I think the comments in the code describe what I am trying to do.

In reply to Reading individual bits by iKnowNothing

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.