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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |