Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Python 'for' loop is memory inefficient
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 41 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Dr. Phillip M. Feldman  
View profile  
 More options Aug 14 2009, 9:25 pm
Newsgroups: comp.lang.python
From: "Dr. Phillip M. Feldman" <pfeld...@verizon.net>
Date: Fri, 14 Aug 2009 18:25:45 -0700 (PDT)
Local: Fri, Aug 14 2009 9:25 pm
Subject: Python 'for' loop is memory inefficient

I wrote the following correct but inefficient test of primality for purposes
of demonstrating that the simplest algorithm is often not the most
efficient.  But, when I try to run the following code with a value of n that
is large enough to produce a significant amount of running time, I get an
out-of-memory error!

def is_prime(n):
   for j in range(2,n):
      if (n % j) == 0: return False
   return True

It seems as though Python is actually expanding range(2,n) into a list of
numbers, even though this is incredibly wasteful of memory. There should be
a looping mechanism that generates the index variable values incrementally
as they are needed.
--
View this message in context: http://www.nabble.com/Python-%27for%27-loop-is-memory-inefficient-tp2...
Sent from the Python - python-list mailing list archive at Nabble.com.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Lawrence  
View profile  
 More options Aug 14 2009, 9:38 pm
Newsgroups: comp.lang.python
From: Mark Lawrence <breamore...@yahoo.co.uk>
Date: Sat, 15 Aug 2009 02:38:18 +0100
Local: Fri, Aug 14 2009 9:38 pm
Subject: Re: Python 'for' loop is memory inefficient
Dr. Phillip M. Feldman wrote:
> I wrote the following correct but inefficient test of primality for purposes
> of demonstrating that the simplest algorithm is often not the most
> efficient.  But, when I try to run the following code with a value of n that
> is large enough to produce a significant amount of running time, I get an
> out-of-memory error!

> def is_prime(n):
>    for j in range(2,n):
>       if (n % j) == 0: return False
>    return True

> It seems as though Python is actually expanding range(2,n) into a list of
> numbers, even though this is incredibly wasteful of memory. There should be
> a looping mechanism that generates the index variable values incrementally
> as they are needed.

I have a strong suspicion that you will find hints in the Python
documentation that this has already been addressed.  Perhaps you could
try reading before posting?

--
Kindest regards.

Mark Lawrence.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ethan Furman  
View profile  
 More options Aug 14 2009, 9:43 pm
Newsgroups: comp.lang.python
From: Ethan Furman <et...@stoneleaf.us>
Date: Fri, 14 Aug 2009 18:43:42 -0700
Local: Fri, Aug 14 2009 9:43 pm
Subject: Re: Python 'for' loop is memory inefficient
Dr. Phillip M. Feldman wrote:

> I wrote the following correct but inefficient test of primality for purposes
> of demonstrating that the simplest algorithm is often not the most
> efficient.  But, when I try to run the following code with a value of n that
> is large enough to produce a significant amount of running time, I get an
> out-of-memory error!

> def is_prime(n):
>    for j in range(2,n):
>       if (n % j) == 0: return False
>    return True

> It seems as though Python is actually expanding range(2,n) into a list of
> numbers, even though this is incredibly wasteful of memory. There should be
> a looping mechanism that generates the index variable values incrementally
> as they are needed.

You already have an answer to the range issue, so I will only add that
putting a loop inside another loop is a decent way to expand the time taken.

I will also observe that if you were to stop programming whatever
language you are more familiar with in Python, and start programming
Python in Python, you'll have an easier time of it.

The Dive Into Python is an excellent start for that.

~Ethan~


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
r  
View profile  
 More options Aug 14 2009, 10:01 pm
Newsgroups: comp.lang.python
From: r <rt8...@gmail.com>
Date: Fri, 14 Aug 2009 19:01:26 -0700 (PDT)
Local: Fri, Aug 14 2009 10:01 pm
Subject: Re: Python 'for' loop is memory inefficient
On Aug 14, 8:25 pm, "Dr. Phillip M. Feldman" <pfeld...@verizon.net>
wrote:

> I wrote the following correct but inefficient test of primality for purposes
> of demonstrating that the simplest algorithm is often not the most
> efficient.  But, when I try to run the following code with a value of n that
> is large enough to produce a significant amount of running time, I get an
> out-of-memory error!

I don't think Python was created to keep mathematician's happy.
Although strangely enough, the creator holds a masters degree in
mathematics *and* computer science. Just another one of life's little
ironies ;)

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rascal  
View profile  
 More options Aug 15 2009, 1:39 am
Newsgroups: comp.lang.python
From: Rascal <jonras...@gmail.com>
Date: Fri, 14 Aug 2009 22:39:08 -0700 (PDT)
Local: Sat, Aug 15 2009 1:39 am
Subject: Re: Python 'for' loop is memory inefficient
 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hendrik van Rooyen  
View profile  
 More options Aug 15 2009, 5:21 am
Newsgroups: comp.lang.python
From: Hendrik van Rooyen <hend...@microcorp.co.za>
Date: Sat, 15 Aug 2009 11:21:12 +0200
Local: Sat, Aug 15 2009 5:21 am
Subject: Re: Python 'for' loop is memory inefficient
On Saturday 15 August 2009 03:25:45 Dr. Phillip M. Feldman wrote:

> It seems as though Python is actually expanding range(2,n) into a list of
> numbers, even though this is incredibly wasteful of memory. There should be
> a looping mechanism that generates the index variable values incrementally
> as they are needed.

There is.

Use xrange instead of range, and try again.

And while you are about it, you may as well teach them that it is much better
to do a multiplication than a division.

-Hendrik


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Machin  
View profile  
 More options Aug 15 2009, 5:20 am
Newsgroups: comp.lang.python
From: John Machin <sjmac...@lexicon.net>
Date: Sat, 15 Aug 2009 02:20:12 -0700 (PDT)
Local: Sat, Aug 15 2009 5:20 am
Subject: Re: Python 'for' loop is memory inefficient
On Aug 15, 11:38 am, Mark Lawrence <breamore...@yahoo.co.uk> wrote:

Alternative hypothesis: the good doctor read the Python 3.x
documentation but absent-mindedly ran Python 2.x

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Aug 15 2009, 8:25 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au>
Date: 15 Aug 2009 12:25:00 GMT
Subject: Re: Python 'for' loop is memory inefficient
On Fri, 14 Aug 2009 18:25:45 -0700, Dr. Phillip M. Feldman wrote:

> It seems as though Python is actually expanding range(2,n) into a list
> of numbers, even though this is incredibly wasteful of memory. There
> should be a looping mechanism that generates the index variable values
> incrementally as they are needed.

Others have already pointed out to you that xrange() (for Python 2.x)
does what you want. In Python 3.x, the old range() is gone for good, and
xrange() renamed to just range().

However, I'd like to point out that your subject line is fundamentally
incorrect. What you've discovered has *nothing* to do with for-loops: the
for-loop will happily iterate over whatever object you pass to it (or at
least try to, because not all objects are iterable). You might be
thinking that Python requires you to write for-loops as

for i in range(...):

but that's not correct. The for-loop doesn't care what object comes after
the "in" and before the ":", so long as it can iterate over it one item
at a time:

for i in [1, 3, 4, 5, 2, 0, -1, 8, 7, 6]:
    print i

for c in "abcd":
    print c

and many other variations will work perfectly fine.

The memory-consumption you have discovered is a property of the range()
function, not the for-loop:

>>> L = range(2, 20)
>>> L

[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Compare to xrange:

>>> L = xrange(2, 20)
>>> L
xrange(2, 20)
>>> list(L)  # expand out into a list

[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

By the way, both range() and xrange() take an optional 'stepsize'
argument, defaulting to 1:

>>> range(3, 20, 2)

[3, 5, 7, 9, 11, 13, 15, 17, 19]

help(range) and help(xrange) in the interactive interpreter are your
friends.

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Nagle  
View profile  
 More options Aug 15 2009, 3:34 pm
Newsgroups: comp.lang.python
From: John Nagle <na...@animats.com>
Date: Sat, 15 Aug 2009 12:34:15 -0700
Local: Sat, Aug 15 2009 3:34 pm
Subject: Re: Python 'for' loop is memory inefficient

Hendrik van Rooyen wrote:
> On Saturday 15 August 2009 03:25:45 Dr. Phillip M. Feldman wrote:
> And while you are about it, you may as well teach them that it is much better
> to do a multiplication than a division.

    Actually, division speed hasn't been much of an issue in years.  Arithmetic
has been faster than memory access since CPUs went superscalar.  In CPython,
with all the interpreter overhead, you'll probably never notice the difference.

    I'd thought "xrange" had already been obsoleted, and hadn't realized the
2.3 - 2.6 versions of CPython were still doing "range" the hard way, not
as a generator.

                                        John Nagle


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MRAB  
View profile  
 More options Aug 15 2009, 5:18 pm
Newsgroups: comp.lang.python
From: MRAB <pyt...@mrabarnett.plus.com>
Date: Sat, 15 Aug 2009 22:18:24 +0100
Local: Sat, Aug 15 2009 5:18 pm
Subject: Re: Python 'for' loop is memory inefficient

It would've broken some existing code, hence it was left until Python 3.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Emmanuel Surleau  
View profile  
 More options Aug 16 2009, 2:30 am
Newsgroups: comp.lang.python
From: Emmanuel Surleau <emmanuel.surl...@gmail.com>
Date: Sun, 16 Aug 2009 08:30:54 +0200
Local: Sun, Aug 16 2009 2:30 am
Subject: Re: Python 'for' loop is memory inefficient

> Dr. Phillip M. Feldman wrote:

[snip]

I don't see what's particularly un-Pythonic with this code. Not using xrange()
is a mistake, certainly, but it remains clear, easily understandable code
which correctly demonstrates the naive algorithm for detecting whether n is a
prime. It doesn't call for condescension

> The Dive Into Python is an excellent start for that.

I never read it, but I was under the impression that the tutorial on
python.org was geared toward programmers moving to Python from other
languages. It was also my impression that Dive Into Python is rather outdated
and occasionally inaccurate (based on comments on this mailing list).

Cheers,

Emm


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Aug 16 2009, 4:32 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au>
Date: 16 Aug 2009 08:32:35 GMT
Local: Sun, Aug 16 2009 4:32 am
Subject: Re: Python 'for' loop is memory inefficient

On Sun, 16 Aug 2009 08:30:54 +0200, Emmanuel Surleau wrote:

[...]

>> I will also observe that if you were to stop programming whatever
>> language you are more familiar with in Python, and start programming
>> Python in Python, you'll have an easier time of it.

> I don't see what's particularly un-Pythonic with this code. Not using
> xrange() is a mistake, certainly, but it remains clear, easily
> understandable code which correctly demonstrates the naive algorithm for
> detecting whether n is a prime. It doesn't call for condescension

It's a particular unfair criticism because the critic (Ethan Furman)
appears to have made a knee-jerk reaction. The "some language in Python"
behaviour he's reacting to is the common idiom:

for i in range(len(seq)):
    do_something_with(seq[i])

instead of the "Python in Python" idiom:

for obj in seq:
    do_something_with(obj)

That's a reasonable criticism, but *not in the case*. Ethan appears to
have made the knee-jerk reaction "for i in range() is Bad" without
stopping to think about what this specific piece of code is actually
doing.

(Replace 'obj' with 'j', 'seq' with 'range(2, n)', and
'do_something_with' with 'if (n % j) == 0: return False', and you have
the exact same code pattern.)

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Emmanuel Surleau  
View profile  
 More options Aug 16 2009, 9:34 am
Newsgroups: comp.lang.python
From: Emmanuel Surleau <emmanuel.surl...@gmail.com>
Date: Sun, 16 Aug 2009 15:34:06 +0200
Local: Sun, Aug 16 2009 9:34 am
Subject: Re: Python 'for' loop is memory inefficient

Fair enough. But as far as I know, for i in (x)range(num) is the canonical way
to iterate over numbers in Python.

Another case of lack of RTFM* before answering, I suppose.

Cheers,

Emm

*Read The Fine Mail


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Benjamin Kaplan  
View profile  
 More options Aug 16 2009, 11:41 am
Newsgroups: comp.lang.python
From: Benjamin Kaplan <benjamin.kap...@case.edu>
Date: Sun, 16 Aug 2009 11:41:21 -0400
Local: Sun, Aug 16 2009 11:41 am
Subject: Re: Python 'for' loop is memory inefficient
On Sun, Aug 16, 2009 at 2:30 AM, Emmanuel Surleau

<emmanuel.surl...@gmail.com> wrote:

> I don't see what's particularly un-Pythonic with this code. Not using xrange()
> is a mistake, certainly, but it remains clear, easily understandable code
> which correctly demonstrates the naive algorithm for detecting whether n is a
> prime. It doesn't call for condescension

It's not that the code is bad, but too many people coming from Java
and C keep thinking of for loops like they're using Java or C and
therefore that "for i in range(a,b)" is identical to "for(int i = a; i
< b; i++)". It's not and, for the most part, you shouldn't code like
that. Since you're using numbers, range/xrange is the appropriate
idiom but you still have to remember that a for loop in python doesn't
loop until a condition is met, it loops through an iterator until the
interator says it's done.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bartc  
View profile  
 More options Aug 16 2009, 2:45 pm
Newsgroups: comp.lang.python
From: "bartc" <ba...@freeuk.com>
Date: Sun, 16 Aug 2009 18:45:27 GMT
Local: Sun, Aug 16 2009 2:45 pm
Subject: Re: Python 'for' loop is memory inefficient

"Steven D'Aprano" <st...@REMOVE-THIS-cybersource.com.au> wrote in message

news:02969972$0$20647$c3e8da3@news.astraweb.com...

> On Fri, 14 Aug 2009 18:25:45 -0700, Dr. Phillip M. Feldman wrote:

>> It seems as though Python is actually expanding range(2,n) into a list
>> of numbers, even though this is incredibly wasteful of memory. There
>> should be a looping mechanism that generates the index variable values
>> incrementally as they are needed.

> Others have already pointed out to you that xrange() (for Python 2.x)
> does what you want. In Python 3.x, the old range() is gone for good, and
> xrange() renamed to just range().

It does seem rather worrying that whoever originally thought up Python
decided it would be a good idea to implement a simple 1 to N (or 0 to N-1)
for-loop by first creating an array of N consecutive integers!

They must have known Python was going to be slow anyway, but this wouldn't
have helped the speed any. Nor the memory consumption.

A for-loop, for iterating over a simple sequence, should be one of the
fastest things in the language.

[Presumably the internal code that created those consecutive integers used a
more conventional looping method...]

--
Bartc


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MRAB  
View profile  
 More options Aug 16 2009, 4:00 pm
Newsgroups: comp.lang.python
From: MRAB <pyt...@mrabarnett.plus.com>
Date: Sun, 16 Aug 2009 21:00:27 +0100
Local: Sun, Aug 16 2009 4:00 pm
Subject: Re: Python 'for' loop is memory inefficient

Whoever? I am shocked! ;-)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sturlamolden  
View profile  
 More options Aug 16 2009, 4:41 pm
Newsgroups: comp.lang.python
From: sturlamolden <sturlamol...@yahoo.no>
Date: Sun, 16 Aug 2009 13:41:07 -0700 (PDT)
Local: Sun, Aug 16 2009 4:41 pm
Subject: Re: Python 'for' loop is memory inefficient
On 16 Aug, 11:45, "bartc" <ba...@freeuk.com> wrote:

> A for-loop, for iterating over a simple sequence, should be one of the
> fastest things in the language.

Anyone experienced with interpreted high-level languages knows this is
not true. Not because iterating a sequence is expensive, but because
the interpreter is constantly executing the body of the loop. There is
a reason why Matlab and Python/NumPy programmers rely heavily on
vectorized array expressions for efficient numerics.

The only thing more evil than a for-loop is recursive function calls.

This does not mean Python is slow. It just means you have to program
differently.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sturlamolden  
View profile  
 More options Aug 16 2009, 6:35 pm
Newsgroups: comp.lang.python
From: sturlamolden <sturlamol...@yahoo.no>
Date: Sun, 16 Aug 2009 15:35:26 -0700 (PDT)
Local: Sun, Aug 16 2009 6:35 pm
Subject: Re: Python 'for' loop is memory inefficient
On 16 Aug, 14:57, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:

>         Well, the alternative would be to have two keywords for looping: one
> for your "simple" incrementing integer loop, and another for a loop that
> operates over the elements of some collection type.

A compiler could easily recognise a statement like

   for i in range(n):

as a simple integer loop. In fact, Cython is able to do this.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Machin  
View profile  
 More options Aug 16 2009, 9:23 pm
Newsgroups: comp.lang.python
From: John Machin <sjmac...@lexicon.net>
Date: Sun, 16 Aug 2009 18:23:23 -0700 (PDT)
Local: Sun, Aug 16 2009 9:23 pm
Subject: Re: Python 'for' loop is memory inefficient
On Aug 17, 8:35 am, sturlamolden <sturlamol...@yahoo.no> wrote:

> A compiler could easily recognise a statement like
>    for i in range(n):
> as a simple integer loop. In fact, Cython is able to do this.

Extremely easy, once users relinquish the right to replace built-in
"range" with their own concoctions ...

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Benjamin Kaplan  
View profile  
 More options Aug 16 2009, 9:23 pm
Newsgroups: comp.lang.python
From: Benjamin Kaplan <benjamin.kap...@case.edu>
Date: Sun, 16 Aug 2009 21:23:20 -0400
Local: Sun, Aug 16 2009 9:23 pm
Subject: Re: Python 'for' loop is memory inefficient

On Sun, Aug 16, 2009 at 6:35 PM, sturlamolden <sturlamol...@yahoo.no> wrote:

> A compiler could easily recognise a statement like

>   for i in range(n):

> as a simple integer loop. In fact, Cython is able to do this.

but special cases aren't special enough to break the rules.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
exar...@twistedmatrix.com  
View profile  
 More options Aug 16 2009, 9:28 pm
Newsgroups: comp.lang.python
From: exar...@twistedmatrix.com
Date: Mon, 17 Aug 2009 01:28:40 -0000
Local: Sun, Aug 16 2009 9:28 pm
Subject: Re: Python 'for' loop is memory inefficient
On 01:23 am, benjamin.kap...@case.edu wrote:

>On Sun, Aug 16, 2009 at 6:35 PM, sturlamolden <sturlamol...@yahoo.no>
>wrote:

>>A compiler could easily recognise a statement like

>>  for i in range(n):

>>as a simple integer loop. In fact, Cython is able to do this.

>but special cases aren't special enough to break the rules.

Although I think PyPy also recognizes this case and makes it as
efficient as using xrange, and does so without breaking any rules.

There *are* _some_ legitimate complaints to be made about the CPython
runtime. :)

Jean-Paul


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Rubin  
View profile  
 More options Aug 16 2009, 9:44 pm
Newsgroups: comp.lang.python
From: Paul Rubin <http://phr...@NOSPAM.invalid>
Date: 16 Aug 2009 18:44:33 -0700
Local: Sun, Aug 16 2009 9:44 pm
Subject: Re: Python 'for' loop is memory inefficient

exar...@twistedmatrix.com writes:
> Although I think PyPy also recognizes this case and makes it as
> efficient as using xrange, and does so without breaking any rules.

How can pypy possibly know that the user hasn't assigned some other
value to "range"?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carl Banks  
View profile  
 More options Aug 16 2009, 9:53 pm
Newsgroups: comp.lang.python
From: Carl Banks <pavlovevide...@gmail.com>
Date: Sun, 16 Aug 2009 18:53:10 -0700 (PDT)
Local: Sun, Aug 16 2009 9:53 pm
Subject: Re: Python 'for' loop is memory inefficient
On Aug 16, 6:28 pm, exar...@twistedmatrix.com wrote:

> On 01:23 am, benjamin.kap...@case.edu wrote:

> >On Sun, Aug 16, 2009 at 6:35 PM, sturlamolden <sturlamol...@yahoo.no>
> >wrote:

> >>A compiler could easily recognise a statement like

> >>  for i in range(n):

> >>as a simple integer loop. In fact, Cython is able to do this.

> >but special cases aren't special enough to break the rules.

> Although I think PyPy also recognizes this case and makes it as
> efficient as using xrange, and does so without breaking any rules.

PyPy uses a JIT compiler (which is still slower than CPython,
suggesting that perhaps they should have spent more time optimizing
the general case than optimizing for an easily avoided special case).

> There *are* _some_ legitimate complaints to be made about the CPython
> runtime. :)

This isn't one of them.

xrange has been part of Python for 10 years.

If there are any complaints to be made about this situation it's that
there are any 2.x learning materials anythere that continue to use
range() and not xrange() in this context.

Carl Banks


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carl Banks  
View profile  
 More options Aug 16 2009, 10:12 pm
Newsgroups: comp.lang.python
From: Carl Banks <pavlovevide...@gmail.com>
Date: Sun, 16 Aug 2009 19:12:20 -0700 (PDT)
Local: Sun, Aug 16 2009 10:12 pm
Subject: Re: Python 'for' loop is memory inefficient
On Aug 16, 3:35 pm, sturlamolden <sturlamol...@yahoo.no> wrote:

> On 16 Aug, 14:57, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:

> >         Well, the alternative would be to have two keywords for looping: one
> > for your "simple" incrementing integer loop, and another for a loop that
> > operates over the elements of some collection type.

> A compiler could easily recognise a statement like

>    for i in range(n):

> as a simple integer loop.

It would be a simple to do if you were writing it for a different
langauge was a lot less dynamic than Python is.  It'd be quite a
complex hack to add it to CPython's compiler while maintaing the
current highly dynamic runtime semantics and backwards compatibility,
which is a design constraint of Python whether you like it or not.

And all this complaining about an issue that can be worked around by
xrange instead of range.  Sheesh.

> In fact, Cython is able to do this.

Cython can do this easily because it is a different language that is a
lot less dynamic than Python.

If you don't care about the dynamic stuff why don't you just use
Cython?  Or quit complaining and just use xrange.

Carl Banks


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nobody  
View profile  
 More options Aug 16 2009, 11:10 pm
Newsgroups: comp.lang.python
From: Nobody <nob...@nowhere.com>
Date: Mon, 17 Aug 2009 04:10:17 +0100
Local: Sun, Aug 16 2009 11:10 pm
Subject: Re: Python 'for' loop is memory inefficient

On Sun, 16 Aug 2009 11:41:21 -0400, Benjamin Kaplan wrote:
> It's not that the code is bad, but too many people coming from Java
> and C keep thinking of for loops like they're using Java or C and
> therefore that "for i in range(a,b)" is identical to "for(int i = a; i
> < b; i++)". It's not and, for the most part, you shouldn't code like
> that. Since you're using numbers, range/xrange is the appropriate
> idiom but you still have to remember that a for loop in python doesn't
> loop until a condition is met, it loops through an iterator until the
> interator says it's done.

Java also has iterators; it's more a case of people coming from C and BASIC.

Although, some of those may have come *through* Java without abandoning
old habits. You see the same thing with people coming from BASIC to C and
writing:

        #define NUM_DATES 50
        int day[NUM_DATES], month[NUM_DATES], year[NUM_DATES];

rather than defining a "struct".

Sometimes referred to as "I know ten languages and can write in BASIC in
all of them".


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 41   Newer >
« Back to Discussions « Newer topic     Older topic »