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 |
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|