In a reply to Netflix (or on handling large amounts of data efficiently in perl), I suggested that Judy::1/Judy1(3) was a sparse bit vector which might be interesting as a replacement for vec(). Perl's built-in vec can also be used as a bit vector but it's not sparse - all bits between the zeroth to the highest ever set are instantiated inside a perl string. This is only convenient if your bit vector isn't too big in ram.
Example code
A simple perl bit vector
use constant MEGABYTE => 2 ** 20; my $vector = ''; # 5,000 bits. Set every 100th bit between 15 million and 20 million. for ( 15_000_000 .. 20_000_000 ) { if ( ! ( $_ % 100 ) ) { vec( $vector, $_, 1 ) = 1; } } printf "%0.1fM\n", length( $vector ) / MEGABYTE;
A simple Judy::1 bit vector
use constant KILOBYTE => 2 ** 10; use Judy::1 qw( Set MemUsed ); my $vec; # 5,000 bits. Set every 100th bit between 15 million and 20 million. for ( 15_000_000 .. 20_000_000 ) { if ( ! ( $_ % 100 ) ) { Set( $judy, $_ ); } } printf "%0.1fK\n", MemUsed( $judy ) / KILOBYTE;
Memory "benchmarks"
Density | vec() | Judy::1 |
---|---|---|
Tests using the entire range 0..20,000,000 | ||
Every bit | 2.3M | 618K |
Density | vec() | Judy::1 |
---|---|---|
Tests using range 15,000,000..20,000,000 | ||
Every bit | 2.3M | 162K |
Every 10th bit | 2.3M | 772K |
Every 100th bit | 2.3M | 158K |
Every 1000th bit | 2.3M | 65K |
⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Compact and sparse bit vector
by demerphq (Chancellor) on Jan 03, 2009 at 13:35 UTC | |
by BrowserUk (Patriarch) on Jan 03, 2009 at 15:00 UTC | |
by demerphq (Chancellor) on Jan 03, 2009 at 18:01 UTC | |
by diotalevi (Canon) on Jan 04, 2009 at 09:30 UTC | |
by BrowserUk (Patriarch) on Jan 04, 2009 at 10:34 UTC |
Back to
Cool Uses for Perl