If speed REALLY matters you do C not perl. But assuming you want Perl......
First OO Perl is 30-40% slower (in my unrelated testing) than straight function calls so you don't want OO (nor might I add is it justified for the task given that you have no real object data to encapsulate ie it just provides syntactic sugar and no functional value). Second you spend memory to gain speed or in other words every disk read is much slower than a memory read ie tens of nanoseconds versus several milliseconds so you want to buffer as big as possible*. Last you do as little as possible, making as few tests as possible, open files once, use global or vars that scope so they don't get created/destroyed, minimize sub calls (on/off stack) etc. I would do something like:
my $file = 'c:/test.pl'; my $BLOCK_SIZE = 1024*1024; open my $fh, $file or die $!; END{close $fh} my ( $buffer, $buf ); read( $fh, $buf, $BLOCK_SIZE ); $buffer .= unpack "B*", $buf; sub get_bits { my ( $num_bits ) = @_; # faster than shift unless ( length($buffer) > $num_bits ) { read( $fh, $buf, $BLOCK_SIZE ); $buffer .= unpack "B*", $buf; die "No more bits left" if length($buffer) < $num_bits; } return substr $buffer, 0, $num_bits, ''; } for ( 1..1000 ) { print get_bits(16), $/; }
* You would play with the BLOCK_SIZE (probably 1-2MB will be optimal as a stab in the dark - see Re: Performance Question for details) to spend as much memory as you can/need to/is optimal and limit disk access. Depends a lot on OS, disks, disk buffering, available memory etc. We make no extra sub calls at all (all that on and off the stack takes time) and just do the minimum. As always YMMV.
cheers
tachyon
In reply to Re: BitStream revisited
by tachyon
in thread BitStream revisited
by spurperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |