I've used the same idiom, BrowserUK.One minor nit, however - use a temp variable, and stringify the array before the grep. Stringifying @array2 for each element of @array1 is probably more expensive than building a hash.
Update: Unless I missed something on my benchmark, the faq is the fastest - by far! I didn't expect it, but I should have ;-) I varied the size of the array and the faq consistently came out on top. The larger @array2 got, the better the faq performed.
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw/cmpthese/;
use vars qw/@array1 @array2/;
sub perlfaq { # lifted from nothingmuch's post above
my %hash;
$hash{$_} = undef foreach (@array2);
@array1 = grep { not exists $hash{$_} } @array1;
}
sub buk {
grep{ local $"="\c1"; -1==index( "\c1@array2\c1", "\c1$_\c1") } @a
+rray1;
}
sub improved {
local $" = "\c1";
my $temp = "\c1@array2\c1";
grep { -1==index($temp, "\c1$_\c1") } @array1;
}
push @array1, int (rand 100) for (1 .. 1000);
push @array2, int (rand 100) for (1 .. 100);
cmpthese (1_000, {
perlfaq => \&perlfaq,
buk => \&buk,
improved => \&improved,
})
__END__
Benchmark: timing 1000 iterations of buk, improved, perlfaq...
buk: 58 wallclock secs (50.99 usr + 0.01 sys = 51.00 CPU) @ 19
+.61/s (n=1
000)
improved: 4 wallclock secs ( 3.98 usr + 0.00 sys = 3.98 CPU) @ 25
+1.51/s (n=
1000)
perlfaq: 1 wallclock secs ( 0.88 usr + 0.00 sys = 0.88 CPU) @ 11
+35.07/s (n
=1000)
Rate buk improved perlfaq
buk 19.6/s -- -92% -98%
improved 252/s 1183% -- -78%
perlfaq 1135/s 5689% 351% --
|