Skip to content

Instantly share code, notes, and snippets.

@moritz
Created March 12, 2012 11:08
Show Gist options
  • Save moritz/2021231 to your computer and use it in GitHub Desktop.
Save moritz/2021231 to your computer and use it in GitHub Desktop.
reverse sort optimizashun?
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util 'shuffle';
use Benchmark 'cmpthese';
my @unsorted = shuffle 1 .. 1000;
sub reverse_sort {
my @sorted = reverse sort @unsorted;
1;
}
sub hack_reverse {
my @sorted = sort {$b <=> $a} @unsorted;
1;
}
cmpthese(-1, {
'reverse sort' => \&reverse_sort,
'hack reverse' => \&hack_reverse,
});
__END__
Rate reverse sort hack reverse
reverse sort 1174/s -- -66%
hack reverse 3413/s 191% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment