use strict; use warnings; use feature 'state'; use CDB_File; use DB_File; use SQLite_File; use Time::HiRes 'time'; my @points = ( [ 0, 0 ], [ -1, -2 ], [ 1, 2 ], [ -1, 2 ], ... ); sub plain_hash { my %hash; $hash{ join(':',@{$_}) } = 1 for @points; \%hash; } sub cdb_hash { my %hash; # create CDB file my $cdb = CDB_File->new("t.cdb", "t.cdb.$$") or die "$!\n"; $cdb->insert( join(':',@{$_}), 1 ) for @points; $cdb->finish; # use CDB file tie %hash, 'CDB_File', "t.cdb" or die "$!\n"; \%hash; } sub db_hash { my %hash; # create DB file tie %hash, 'DB_File', "t.db", O_CREAT|O_RDWR, $DB_BTREE; $hash{ join(':',@{$_}) } = 1 for @points; untie %hash; # use DB file tie %hash, 'DB_File', "t.db", O_RDWR, $DB_BTREE; \%hash; } sub sql_hash { tie my %hash, 'SQLite_File', "sql.db"; $hash{ join(':',@{$_}) } = 1 for @points; \%hash; } sub look { my $cells = shift; state $points_str = [ map { join(':',@{$_}) } @points ]; for my $p (@{ $points_str }) { exists $cells->{$p} or die; } } sub bench { my ( $desc, $func ) = @_; my ( $cells, $iters ) = ( $func->(), 50000 ); my $start = time; look($cells) for 1..$iters; my $elapse = time - $start; printf "%s duration : %0.03f secs\n", $desc, $elapse; printf "%s lookups : %d / sec\n", $desc, @points * $iters / $elapse; } bench( "plain", \&plain_hash ); bench( " cdb", \&cdb_hash ); bench( " db", \&db_hash ); bench( " sql", \&sql_hash );