use strict; use warnings; use Benchmark qw( cmpthese ); use List::Util qw( shuffle ); use constant COUNT => $ARGV[0]; use constant TIME => $ARGV[1]; our @unsorted = shuffle map "$_", 1..COUNT; my $neg_step = ' use strict; use warnings; our @unsorted; my @output; my @sorted = sort @unsorted; for (my $i=$#sorted; $i>=0; $i--) { push(@output, $sorted[$i]); } 1; '; my $neg_i = ' use strict; use warnings; our @unsorted; my @output; my @sorted = sort @unsorted; for my $i (-$#sorted..0) { push(@output, $sorted[$i]); } 1; '; my $b_cmp_a = ' use strict; use warnings; our @unsorted; my @output; my @sorted = sort { $b cmp $a } @unsorted; for my $i (0..$#sorted) { push(@output, $sorted[$i]); } 1; '; my $reversed = ' use strict; use warnings; our @unsorted; my @output; my @sorted = reverse sort @unsorted; for my $i (0..$#sorted) { push(@output, $sorted[$i]); } 1; '; cmpthese(TIME, { neg_step => $neg_step, neg_i => $neg_i, b_cmp_a => $b_cmp_a, reversed => $reversed, });