Created
March 12, 2012 11:08
-
-
Save moritz/2021231 to your computer and use it in GitHub Desktop.
reverse sort optimizashun?
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 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