If performance is paramount in your application, unpacking every bit to a byte, thereby compounding the memory requirements x8, and then processing the 'bits' one at a time is so not the way to approach the problem.

Adding the overhead, both performance and memory, of a perlish OO interface is just compounding this further.

Perl's vec builtin allows read/write access to the bits, individually and in groups, of bitstrings of arbitrary (given you have the memory) length. It effectively allows you to treat the string as a huge array of bits, getting and setting them, by index, in-situ.

By comparison with your mechanism, it is both fast and efficient, and requires no extra memory beyond the bitstring itself. The only time vec becomes a little awkward to use is if you need to access multiples of bits that are not powers of two and/or cross byte boundaries. But there are very few file formats that do not align the bits on (at least) byte boundaries, so that is very rarely a problem.

You don't describe the actual application, but I would highly recommend that you take another look at vec and understand what does and how it works. You will probably find that you can re-cast your problem to use it and greatly improve the efficency by doing so.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!


In reply to Re: BitStream revisited by BrowserUk
in thread BitStream revisited by spurperl

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.