in reply to Locating a specified number of contiguous 0 bits within a large bitstring efficiently.
To avoid questions of byte boundary alignment you could perhaps convert the vector to a character string and use regex matching with @- to find your offset. This seems to work quite quickly unless you look for a contiguous block of a length that doesn't actually exist. On my fairly old laptop that will take a little while before it decides it can't find the block.
use strict; use warnings; use 5.014; use List::Util qw{ max }; use Time::HiRes qw{ gettimeofday tv_interval }; my $t0 = [ gettimeofday() ]; srand 1234567; my $vec = q{}; vec( $vec, 536_870_911, 1 ) = 0; vec( $vec, int rand 536_870_911, 1 ) = 1 for 1 .. 1e7; my $t1 = [ gettimeofday() ]; say qq{Creating vector - @{ [ tv_interval( $t0, $t1 ) ] }}; my $bitStr = unpack q{b*}, $vec; my $t2 = [ gettimeofday() ]; say qq{Unpacking bitstring - @{ [ tv_interval( $t1, $t2 ) ] }}; say qq{Longest contiguous block of zeros is }, max map length, $bitStr =~ m{(0+)}g, q{ bits long}; my $t3 = [ gettimeofday() ]; say qq{Finding longest block - @{ [ tv_interval( $t2, $t3 ) ] }}; for my $numZeros ( 25, 78, 307, 599, 943 ) { my $ts = [ gettimeofday() ]; say qq{At least $numZeros contiguous 0s }, $bitStr =~ m{(0{$numZeros,})} ? qq{found at offset $-[ 0 ], length @{ [ length $1 ] }} : q{could not be found}; say qq{ Search took - @{ [ tv_interval( $ts, [ gettimeofday() +] ) ] }}; }
The output.
Creating vector - 6.612482 Unpacking bitstring - 2.121185 Longest contiguous block of zeros is 843 Finding longest block - 9.848668 At least 25 contiguous 0s found at offset 0, length 37 Search took - 1.685613 At least 78 contiguous 0s found at offset 134, length 85 Search took - 0.725265 At least 307 contiguous 0s found at offset 31289, length 343 Search took - 0.702597 At least 599 contiguous 0s found at offset 5476471, length 625 Search took - 0.82269 At least 943 contiguous 0s could not be found Search took - 12.095307
I don't know whether this method will be fast enough for your purposes but I think it is likely to be simpler that juggling byte boundaries. I hope this will be of use.
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Locating a specified number of contiguous 0 bits within a large bitstring efficiently.
by BrowserUk (Patriarch) on Jun 07, 2013 at 03:29 UTC | |
by johngg (Canon) on Jun 07, 2013 at 16:48 UTC | |
by BrowserUk (Patriarch) on Jun 07, 2013 at 20:34 UTC |