Created
March 7, 2010 22:48
-
-
Save brunoV/324697 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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