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% --
|