Does anyone have a faster way than using a cursor as in the example below?
If testing, note that the first time you run the script it's creating the db so that's not a legitimate case.
One thing I noticed is that the time to build the array is dependent on the data size. When I change "x 1000" to "x 4000" the time to build the array goes from 0.8 seconds to 1.6. I guess that surprises me because I imagined I wouldn't be hitting the data pages in the db, only the index pages. But I suppose only the hashed key is kept there and not the actual key.
use BerkeleyDB;
$create = ! -f 'data.dbm';
$db = new BerkeleyDB::Btree (
-Filename => 'data.dbm',
-Flags => DB_CREATE )
or die "Cannot open file: $!";
if ( $create )
{
for ( my $i = 1; $i <= 100000; $i++ )
{
$db->db_put ( $i, "data" x 1000 );
}
}
$cursor = $db->db_cursor ( );
@keys = ( );
$key = 0;
$value = 0;
$status;
for (
$status = $cursor->c_get ( $key, $value, DB_FIRST );
$status == 0;
$status = $cursor->c_get ( $key, $value, DB_NEXT ) )
{
push @keys, $key;
}
$cursor->c_close ( );
$db->db_close ( );
print ( $#keys + 1 ) . "\n";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.