in reply to Re: Re: Re: Set Operators
in thread Set Operators

$hash{@array} is wrong, you want @hash{@array}.

But since you're (rightly) using exists, you can reduce that even further:

@hash{@array} = (); Some quick testing shows this is about 25% faster:
#!/usr/bin/perl -w use strict; use Benchmark; sub is_elem_of_A { my ($x, %hash) = shift; @hash{@_} = (1) x @_; exists $hash{$x} } sub is_elem_of_B { my ($x, %hash) = shift; @hash{@_} = (); exists $hash{$x} } my %param = ( short => [ qw(one one two three four five) ], long => [ 10, 1 .. 10000 ], ); for(qw(short long)) { print "Timing with $_ list\n"; my $list = $param{$_}; timethese -5, { xop => sub{ is_elem_of_A @$list }, emptylist => sub{ is_elem_of_B @$list }, }; } __END__ Timing with short list Benchmark: running emptylist, xop, each for at least 5 CPU seconds... emptylist: 6 wallclock secs ( 5.29 usr + 0.04 sys = 5.33 CPU) @ 42 +362.48/s (n=225792) xop: 6 wallclock secs ( 5.35 usr + 0.04 sys = 5.39 CPU) @ 31 +733.58/s (n=171044) Timing with long list Benchmark: running emptylist, xop, each for at least 5 CPU seconds... emptylist: 5 wallclock secs ( 5.28 usr + 0.01 sys = 5.29 CPU) @ 22 +.12/s (n=117) xop: 6 wallclock secs ( 5.50 usr + 0.02 sys = 5.52 CPU) @ 15 +.40/s (n=85)

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re^4: Set Operators
by DaveH (Monk) on Oct 27, 2002 at 15:54 UTC

    Just caught that one myself! :-)

    I've updated my example - as it now shows, the 'xop' is not that much faster than just iterating over the array with a properly constructed 'for' loop. In fact, in this example, 'grep' has turned out to be the most consistent performer (but that might be because there is no overhead in creating a hash). Grep is probably fastest because it is coded directly in C, rather than including any extra Perl contructs. Just goes to show that you should always test and test again before you go proclaiming one method over another! :-) (Serves me right for trying to be a smart-ass!)

    Cheers,

    -- Dave :-)

      Obviously, as this task is exactly what grep was made for. :-) Although there is some Perl construct involved - the callback you have to provide to grep.

      Remember that you have to iterate once over the array at least once - whatever you do. grep does nothing more than iterate - where the hash methods have to do a lot more work.

      This doesn't mean grep is the be-all end-all solution: if you need to check many values against the same set, then constructing the hash will quickly pay off as all subsequent lookups run in nearly constant time. Esp when the set is large and even more so when the values are typically absent more often than not, the overhead of building the hash will pay off rapidly.

      Efficiency can only be achieved with knowledge of an algorithm's expected input data.

      Makeshifts last the longest.

        This doesn't mean grep is the be-all end-all solution: if you need to check many values against the same set, then constructing the hash will quickly pay off as all subsequent lookups run in nearly constant time. Esp when the set is large and even more so when the values are typically absent more often than not, the overhead of building the hash will pay off rapidly.

        Indeed, this was the case I had in mind. I guess it comes down to which is more readable,

         my %items = map {$_ => 1} qw (one two three);

        or:

        my %hash; @hash{qw(one two three)} = ();

        I'm still not clear on why that last one works...