in reply to Set::Scalar saves you from hash acrobatics

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

Replies are listed 'Best First'.
Re: Re: Set::Scalar saves you from hash acrobatics
by zby (Vicar) on Oct 07, 2003 at 09:22 UTC
    I don't have a book on algorithms and data structers by hand (and googling did not render any usefull result) but I remember from my education that there are data structers for set operations with slightly better complexity characteristics than hash.

    By the way they should make a note in the documentation of this module on the algorithm they use.

A reply falls below the community's threshold of quality. You may see it by logging in.