in reply to How do I efficiently predeclare a hash with a set of keys
I was going to suggest that depending on the sparseness of your keys, and whether they are all positive integers, it might be a faster to use exists to test an array. Before I made that assertion, I thought I should write a benchmark.
The results were that the theoretical optimization is pretty useless. The speeds were basically identical.
So, unless someone cares to tell me how badly I screwed up my benchmark, (proper benchmarking is hard, and I don't write them often) I'd have to say "Don't bother with my silly array idea."
Update: BrowserUK was kind enough to demonstrate that my benchmarks were flawed and that the arrays are indeed about 37% faster.
use strict; use warnings; use Benchmark; use List::Util qw(shuffle); my $count=5000000; my @existing_indexes = ( 5,6,8..53,62..106); my @indexes = (0..200); my @shuffled_indexes = shuffle @indexes; my %test_hash; my @test_array; @test_array[ @existing_indexes ] = (); @test_hash{ @existing_indexes } = (); Benchmark::cmpthese($count, { arrayLookup => q{ my $val = &arrayLookup( \@test_array, \@indexes ); }, hashLookup => q{ my $val = &hashLookup( \%test_hash, \@indexes ); }, shuffled_arrayLookup => q{ my $val = &arrayLookup( \@test_array, \@shuffled_indexes ); }, shuffled_hashLookup => q{ my $val = &hashLookup( \%test_hash, \@shuffled_indexes ) }, } ); sub arrayLookup { my $array = shift; my $indexes = shift; my @results = map { exists $array->[$_]; } @$indexes; return \@results; } sub hashLookup { my $hash = shift; my $keys = shift; my @results = map { exists $hash->{$_}; } @$keys; return \@results; }
And the results
Run 1: Rate shuffled_arrayLookup hashLookup shuffled +_hashLookup arrayLookup shuffled_arrayLookup 409199/s -- -1% + -1% -2% hashLookup 411286/s 1% -- + -1% -2% shuffled_hashLookup 415076/s 1% 1% + -- -1% arrayLookup 417781/s 2% 2% + 1% -- Run 2: shuffled_arrayLookup arrayLookup shuffled_hashLookup hashLookup 412371/s -- -0% +-1% -1% shuffled_arrayLookup 413976/s 0% -- +-0% -1% arrayLookup 414491/s 1% 0% + -- -1% shuffled_hashLookup 416667/s 1% 1% + 1% -- C:\Documents and Settings\Mark\Desktop\Downloads> exists.pl Rate shuffled_arrayLookup hashLookup shuffled +_hashLookup arrayLookup shuffled_arrayLookup 409199/s -- -1% + -1% -2% hashLookup 411286/s 1% -- + -1% -2% shuffled_hashLookup 415076/s 1% 1% + -- -1% arrayLookup 417781/s 2% 2% + 1% -- Run 3: Rate hashLookup shuffled_arrayLookup arrayLoo +kup shuffled_hashLookup hashLookup 412371/s -- -0% +-1% -1% shuffled_arrayLookup 413976/s 0% -- +-0% -1% arrayLookup 414491/s 1% 0% + -- -1% shuffled_hashLookup 416667/s 1% 1% + 1% --
TGI says moo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How do I efficiently predeclare a hash with a set of keys
by BrowserUk (Patriarch) on May 18, 2007 at 23:29 UTC | |
|
Re^2: How do I efficiently predeclare a hash with a set of keys
by BrowserUk (Patriarch) on May 18, 2007 at 23:17 UTC |