in reply to Fast, Efficient Union and Intersection on arrays

Thanks all for a fabulous lessons in efficiency and creativity. I implemented BrowserUK's vector routine, and Roy Johnson's single grep routine - and at 100,000 iterations with moderately sized arrays (mean 151, stdev 185) the winner is:
             Wall Time
barvin       54.134s
BrowserUK    26.905s
Roy Johnson  21.349s

So for those coming in by Google...

To calculate the count of intersection and union on two arrays (used by me to calculate the Jaccard Distance on the edges of a graph), pass two array references that have no internal duplicates to Roy's subroutine below. Thanks Roy.

sub get_int_uni2 {
  my ($in, $jn) = @_;
  my %i;
  @i{@$in} = ();
  my $int = grep exists $i{$_}, @$jn;
  return ($int, @$in + @$jn - $int);
}
  • Comment on Re: Fast, Efficient Union and Intersection on arrays

Replies are listed 'Best First'.
Re^2: Fast, Efficient Union and Intersection on arrays
by perreal (Monk) on Nov 20, 2008 at 21:34 UTC
    If you can maintain sorted structures, which is painful in Perl, then perhaps you can speed up more.