in reply to Re^3: fast simple DB (sqlite?) skeleton?
in thread fast simple DB (sqlite?) skeleton?
First, note the performance gain from preprocessing.
And here is the codes/iter original preprocess original 3.26 -- -93% preprocess 0.221 1375% --
#!/usr/bin/perl use strict; use warnings; use Benchmark ':hireswallclock'; use DB_File (); my $DB_FILE1; my $DB_FILE2; my $NUM_RECORDS = 50_000; my $NUM_KEYS = 1000; setup_dbfile(); Benchmark::cmpthese( 10, { 'original' => \&benchmark_dbfile1, 'preprocess' => \&benchmark_dbfile2 } ); sub benchmark_dbfile1 { foreach my $value ( 1 .. $NUM_RECORDS ) { my $key = int(rand($NUM_KEYS)); if ( exists $DB_FILE1->{$key} ) { $DB_FILE1->{$key} .= ",$value"; } else { $DB_FILE1->{$key} = $value; } } } sub benchmark_dbfile2 { my %preprocess = (); foreach my $value ( 1 .. $NUM_RECORDS ) { my $key = int(rand($NUM_KEYS)); push @{ $preprocess{$key} }, $value ; } while (my ($key, $val_list) = each %preprocess) { my $value = join ",", @$val_list; if ( exists $DB_FILE2->{$key} ) { $DB_FILE1->{$key} .= ",$value"; } else { $DB_FILE2->{$key} = $value; } } } sub setup_dbfile { { unlink 'berkeley.db1'; my %data; tie %data, 'DB_File', 'berkeley.db1' or die "$!"; $DB_FILE1 = \%data; } { unlink 'berkeley.db2'; my %data; tie %data, 'DB_File', 'berkeley.db2' or die "$!"; $DB_FILE2 = \%data; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: fast simple DB (sqlite?) skeleton?
by WizardOfUz (Friar) on Jan 28, 2010 at 13:11 UTC | |
by pmqs (Friar) on Jan 29, 2010 at 10:11 UTC |