Skip to content

Instantly share code, notes, and snippets.

@brunoV
Created March 7, 2010 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunoV/324697 to your computer and use it in GitHub Desktop.
Save brunoV/324697 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Benchmark 'cmpthese';
use perl5i::latest;
my @things = map { rand_number() } (1 .. 100);
my @thangs = map { rand_number() } (1 .. 100);
sub ARRAY::diff_simple {
my ($this, $that) = @_;
my %seen = map { $_ => 1 } @$that;
my @result = grep { not $seen{$_} } @$this;
return wantarray ? @result : \@result;
}
cmpthese(
-2,
{
perl5i => sub { @things->diff( \@thangs ) },
diff_simple => sub { @things->diff_simple( \@thangs ) },
}
);
sub rand_number {
return sprintf("%.3f", rand());
}
__END__
Before:
Rate perl5i diff_simple
perl5i 22.5/s -- -100%
diff_simple 8675/s 38455% --
After:
Rate perl5i diff_simple
perl5i 8330/s -- -12%
diff_simple 9457/s 14% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment