memory efficiency - for loop with reverse list

T

tuser

I am reading Damian Conways "Perl Best Practices", in chapter 8
(Built-in Functions), paragraph "reversing lists", he discusses coding
a down-iterating for loop:

I quote from the book
===========================
....
===========================

I understand the benefits of the second loop and Damian states that the
difference in iteration speed is usually not even noticable.

But I can't find any statement about memory efficiency, so my question
is this:

In the second for-loop (the one with the reverse-function):

would the entire list ($MIN..$MAX) be constructed first, then reversed
and iterated over ?

....or...

is there some optimizer which internally would translate the for-loop
into a memory efficient for (my $remaining=$MAX; $remaining>=$MIN;
$remaining--) ?
 
T

tuser

James said:
In recent versions of perl (accd to perldoc perlop, under the section
Range Operators), the increasing case:

foreach (1 .. 1_000_000)
{
sleep 5;
}

... does not build the list in memory, which I verified using top.
(perl did not show up in the first 60 lines of top, sorted by mem). The
reverse case, however:

foreach (reverse 1 .. 1_000_000)
{
sleep 5;
}

Used 9.4% of my memory (second only to firefox), using perl 5.8.7

ymmv

Thanks very much for your answer.

I am running activestate perl v5.8.7 (Windows XP), is there a utility
under Windows XP with which I can test the memory usage myself ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top