Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.


In reply to Re: For Loops and Reversing output by ikegami
in thread For Loops and Reversing output by brusimm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (7)
    As of 2014-08-20 11:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (111 votes), past polls