in reply to Re^2: Fast, Efficient Union and Intersection on arrays
in thread Fast, Efficient Union and Intersection on arrays

I remember one time getting a noticeable speed improvement by using the 'for (...;...;...)' over a foreach loop for a large array. I wondering if that would have helped here.
  • Comment on Re^3: Fast, Efficient Union and Intersection on arrays

Replies are listed 'Best First'.
Re^4: Fast, Efficient Union and Intersection on arrays
by TGI (Parson) on Nov 20, 2008 at 22:22 UTC

    Once again, expectations are shattered: the c style for loop is the slowest.

    sub one_cfor { # my (@a, @b); # initialize @a and @b here my %hash; @hash{@a} = (1) x @a; my @union = @a; my @intersection; for (my $i=0; $i<$#b; $i++ ) { my $bval = $b[$i]; if( exists $hash{$bval} ) { push @intersection, $bval } else { push @union, $bval } } # print "union @union\n"; # print "interstection @intersection\n"; } ################ Range (1,50) vs (3,80) Rate one_cfor one_for two_greps one_cfor 4438/s -- -24% -30% one_for 5834/s 31% -- -8% two_greps 6312/s 42% 8% -- Range (1,5) vs (3,8) Rate one_cfor one_for two_greps one_cfor 50000/s -- -13% -29% one_for 57637/s 15% -- -18% two_greps 70323/s 41% 22% --


    TGI says moo