Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-03-19 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found