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

Re: For Loops and Reversing output

by ikegami (Pope)
on Dec 12, 2006 at 00:33 UTC ( #589197=note: print w/ replies, xml ) Need Help??


in reply to For Loops and Reversing output

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.


Comment on Re: For Loops and Reversing output
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://589197]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-07-26 18:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (178 votes), past polls