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

Re^2: For Loops and Reversing output

by ikegami (Pope)
on Dec 13, 2006 at 06:31 UTC ( #589496=note: print w/ replies, xml ) Need Help??


in reply to Re: For Loops and Reversing output
in thread For Loops and Reversing output

reverse sort uses no more memory than sort.

I've showed earlier in this thread that reverse sort is just as fast as sort. That's because an optimization causes reverse sort to sort the list in the reverse order rather than sorting the list then reversing it. That also means that reverse sort uses no more memory than sort.

However, reversing an already sorted list takes more time and memory than not reversing it.

Based on a reply the OP made, I suspect he will be receiving the list already sorted in the reverse of the desired order. If that's the case, then reversing the list then iterating over it would take time and memory, while iterating over the list from end to start could avoid using time and memory.

Compare

my @reversed = reverse @sorted; for my $i (0..$#reversed) { print $reversed[$i], "\n"; }

against

for (my $i=$#sorted; $i>=0; $i--) { print $sorted[$i], "\n"; }


Comment on Re^2: 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://589496]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (15)
As of 2014-07-30 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (232 votes), past polls