FYI, here was my test (assuming worst case of 'not found'):
#!/usr/local/bin/perl -l -w use strict; use Benchmark; my @zips = qw( 92713 92714 92715 92716 92717 92718 92719 ); my $zip = '11111'; timethese (50000,{ HASH => \&hash_it, LINEAR => \&linear, LINEAR_GREP => \&linear_grep, }); sub hash_it { my %zips; @zips{@zips} = undef; exists $zips{$zip}; } sub linear { for (@zips) { return 1 if $_ eq $zip; } return 0; } sub linear_grep { grep { $_ eq $zip } @zips; } /usr/home/dougw/tst>./timeit Benchmark: timing 50000 iterations of HASH, LINEAR, LINEAR_GREP ... HASH: 3 wallclock secs ( 2.65 usr + 0.01 sys = 2.66 CPU) @ 18 +796.99/s (n=50000) LINEAR: 1 wallclock secs ( 1.84 usr + 0.00 sys = 1.84 CPU) @ 27 +173.91/s (n=50000) LINEAR_GREP: 0 wallclock secs ( 1.16 usr + 0.00 sys = 1.16 CPU) @ 4 +3103.45/s (n=50000)
Update:I don't include positive searches because we don't know the probability of WebSmart's users of entering a 'found' zip code. So I assume that since there's so few (5 - 10) 'valid' zip codes, and so many possible zip codes to enter, it will probably be 'not found'. Positive searches would just make the results lean more toward the LINEAR search, I don't know how many positive searches it would take before LINEAR would be better than the LINEAR_GREP. Either is better than the HASH, though.

In reply to Re: Re: Re: Re: Zip Code Script by runrig
in thread Zip Code Script by WebSmart

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.