in reply to BitStream revisited

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!

Replies are listed 'Best First'.
Re: Re: BitStream revisited
by ysth (Canon) on Dec 30, 2003 at 19:20 UTC
    It sounds like some kind of compression algoritm to me, so I wouldn't be surprised if the lengths were arbitrary. If that is the case, I think avoiding vec is preferable.

    Update: but I'd love to see someone come up with example code using vec that handled non-power-of-2 lengths.