Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: For Loops and Reversing output

by chrism01 (Friar)
on Dec 13, 2006 at 05:49 UTC ( #589493=note: print w/replies, xml ) Need Help??


in reply to For Loops and Reversing output

I think people thought 'homework', as did I, because not being allowed to use 'reverse' sounds like a completely arbitrary restriction, typical of academic qns designed to force you to learn more about the lang.
Nothing wrong with that in that env, of course, but definitely unusual in real world.
I'm guessing that maybe the amt of data involved in the up-coming job is extremely large & using reverse would cause perf/mem issues? Maybe embedded code?
Cheers
Chris

Replies are listed 'Best First'.
Re^2: For Loops and Reversing output
by ikegami (Pope) on Dec 13, 2006 at 06:31 UTC

    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"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://589493]
help
Chatterbox?
[stevieb]: interestingly enough, someone else got my Devel::Examine:: Subs distribution for their PRC, and I applaud the change. This dist is extremely complicated and mostly obfu, but the person doing it understood PPI enough to change...
[stevieb]: ...something I had overlooked in the extreme depths of the core functionality. After merging, then a couple of extra tweaks, I still have 100% test coverage. Yay for people who write tests!

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2017-01-24 01:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (199 votes). Check out past polls.