use Benchmark; use Set::Scalar; my $stopwords = Set::Scalar->new; $stopwords->insert( qw(a i at be do to or is not no the that they then these them who where why can find on an of and it by)); my %stopwords; @stopwords{qw(a i at be do to or is not no the that they then these them who where why can find on an of and it by)} = (); @word = split /\s+/, "Oh say can you see by the dawn's early light"; sub withSetScalar { my @salient_word = grep { not $stopwords->has($_) } @word; } sub byHash { my @salient_word = grep { not exists $stopwords{$_} } @word; } timethese 100000, { withSetScalar => \&withSetScalar, byHash => \&byHash, }; __END__ Benchmark: timing 100000 iterations of byHash, withSetScalar... byHash: 1 wallclock secs ( 1.06 usr + 0.00 sys = 1.06 CPU) @ 94 +161.96/s (n=100000) withSetScalar: 9 wallclock secs ( 8.05 usr + 0.01 sys = 8.06 CPU) @ + 12403.87/s (n=100000)

I don't think the "nicer" syntax is worth the price. (If the sentence/text was longer the difference would be even bigger.)

Jenda
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
   -- Rick Osborne

Edit by castaway: Closed small tag in signature


In reply to Re: Set::Scalar saves you from hash acrobatics by Jenda
in thread Set::Scalar saves you from hash acrobatics by princepawn

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.