my @sorted = sort @unsorted;
for (my $i=$#sorted; $i>=0; $i--) {
print $sorted[$i], "\n";
}
However,
my @sorted = reverse sort @unsorted;
is optimized to be just as fast as
my @sorted = sort @unsorted;
so you're making your program less readable for nothing by avoiding reverse.
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,
});
outputs
>perl 589197.pl 1000 -3
Rate neg_step b_cmp_a reversed neg_i
neg_step 301/s -- -0% -1% -1%
b_cmp_a 302/s 0% -- -1% -1%
reversed 304/s 1% 1% -- -0%
neg_i 304/s 1% 1% 0% --
>perl 589197.pl 1000 -3
Rate b_cmp_a neg_step reversed neg_i
b_cmp_a 301/s -- -1% -2% -2%
neg_step 303/s 1% -- -1% -1%
reversed 306/s 2% 1% -- -0%
neg_i 306/s 2% 1% 0% --
>perl 589197.pl 1000 -3
Rate neg_step b_cmp_a neg_i reversed
neg_step 301/s -- -0% -1% -2%
b_cmp_a 302/s 0% -- -1% -1%
neg_i 305/s 1% 1% -- -0%
reversed 306/s 2% 1% 0% --
>perl 589197.pl 10000 -5
Rate b_cmp_a neg_step neg_i reversed
b_cmp_a 22.4/s -- -1% -2% -3%
neg_step 22.6/s 1% -- -2% -2%
neg_i 23.0/s 2% 2% -- -0%
reversed 23.0/s 3% 2% 0% --
>perl 589197.pl 10000 -5
Rate neg_step b_cmp_a reversed neg_i
neg_step 22.5/s -- -0% -2% -2%
b_cmp_a 22.5/s 0% -- -2% -2%
reversed 22.9/s 2% 2% -- -0%
neg_i 23.0/s 2% 2% 0% --
>perl 589197.pl 10000 -5
Rate b_cmp_a neg_step neg_i reversed
b_cmp_a 22.5/s -- -1% -2% -2%
neg_step 22.7/s 1% -- -1% -1%
neg_i 22.8/s 2% 1% -- -1%
reversed 23.0/s 2% 1% 1% --
As you can see, none is faster than any other, so pick the one that's more readable and maintainable.
Update: Oops! Changed $i++ to $i--.
Update: Added benchmarks.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|