It all depends on how many things you are looking up, and how many in the match tree.

use strict; use warnings; testit(100000,5); testit(100000,50); testit(1000000,5); testit(1000000,50); sub testit { my $inputsize=shift; my $testn=shift; my @list; for my $i(1..$inputsize) { push @list,sprintf('%010d',$i); } my @lookfors; for my $i(1..$testn) { push @lookfors,sprintf('%010d',$i); } my %lookup; for my $lf (@lookfors){ $lookup{$lf}=1; } { my $type='hash'; my $st=time; my $ct=0; for my $test (@list) { unless ($lookup{$test}) {$ct++;} } my $et=time-$st; printf "%5s inputsize:%10d testn:%5d ct:%10d time:%3d \n",$type,$inp +utsize,$testn,$ct,$et; } { # approx an if/the/elsif/else tree my $type='list'; my $st=time; my $ct=0; tests: for my $test (@list) { for my $lf(@lookfors){ if ($test eq $lf) {next tests; } } $ct++; } my $et=time-$st; printf "%5s inputsize:%10d testn:%5d ct:%10d time:%3d \n",$type,$inp +utsize,$testn,$ct,$et; } }
result
hash inputsize: 100000 testn: 5 ct: 99995 time: 0 list inputsize: 100000 testn: 5 ct: 99995 time: 0 hash inputsize: 100000 testn: 50 ct: 99950 time: 0 list inputsize: 100000 testn: 50 ct: 99950 time: 1 hash inputsize: 1000000 testn: 5 ct: 999995 time: 0 list inputsize: 1000000 testn: 5 ct: 999995 time: 2 hash inputsize: 1000000 testn: 50 ct: 999950 time: 0 list inputsize: 1000000 testn: 50 ct: 999950 time: 10
As you an see for a small number of items to test against it doesnt make much of a difference. I would use the hash just because the code is cleaner.


In reply to Re^3: How to add more conditional statements in an efficient manner by huck
in thread How to add more conditional statements in an efficient manner by adamZ88

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.