One thousand pardons, m'lord japhy, but you wrote:

But if you do this a lot of times, you're kissing inefficiency on its ratty lips. Use a hash:

In the oft-maligned name of pragmatism, I cobbled together the following brief, unscientific test:

#!/usr/bin/perl -w use Benchmark; @List = ("aaaa" .. "zzzz"); print "Elements:", scalar @List, "\n"; sub first (&@) { my $cref = shift; $cref->($_) and return $_ for @_; } $t0 = new Benchmark; $first_match = first {$_ eq "zzzz"} @List; $t1 = new Benchmark; $td = timediff($t1, $t0); print "Found it! (sub)\n" if $first_match; print "sub took: ", timestr($td), "\n"; # Crufty way to figure out how big I am on a Linux box - /msg me with +improvements, please! -McD $size = (split(" ", `ps -hlp $$`))[6]; print "Size: $size\n"; $t0 = new Benchmark; my $Found=0; for (@List) { if ($_ eq "zzzz") { $Found=1; last; } } $t1 = new Benchmark; $td = timediff($t1, $t0); print "Found it! (scan)\n" if $Found; print "scan took: ", timestr($td), "\n"; $size = (split(" ", `ps -hlp $$`))[6]; print "Size: $size\n"; $t0 = new Benchmark; my %seen; @seen{@List} = (); $t1 = new Benchmark; $td = timediff($t1, $t0); if (exists $seen{"zzzz"}) { print "Found it! (hash)\n"; } print "hash took: ", timestr($td), "\n"; $size = (split(" ", `ps -hlp $$`))[6]; print "Size: $size\n";
To make this a worst-case for the linear teams, I searched for the last item in the list.

As it turns out, the least appealing code is the fastest - but while the hash approach may not be kissing performance inefficiency on it's ratty lips, it's certainly been caught in some kind of carnal embrace with memory inefficiency.

Here are the results on my box:

./existance.pl
Elements:456976
Found it! (sub)
sub took:  2 wallclock secs ( 1.49 usr +  0.01 sys =  1.50 CPU)
Size: 58632
Found it! (scan)
scan took:  1 wallclock secs ( 0.40 usr +  0.00 sys =  0.40 CPU)
Size: 58632
Found it! (hash)
hash took:  2 wallclock secs ( 0.90 usr +  0.15 sys =  1.05 CPU)
Size: 88324
Of course, we've strayed far from meditation and into experimentation. I'm sorry, what were we optimizing for again? :-)

Peace,
-McD


In reply to Re: Re: Beauty is in the eye of the beholder by McD
in thread Beauty is in the eye of the beholder by ChOas

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.