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


In reply to Re: How do I efficiently predeclare a hash with a set of keys by TGI
in thread How do I efficiently predeclare a hash with a set of keys by lupey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.