Re: python loops

Discussion in 'Python' started by Fredrik Lundh, Sep 1, 2006.

  1. wrote:

    > I thought the xrange was preferred? for x in xrange(length):


    preferred by premature optimization freaks, perhaps. in practice, if the
    range is reasonably small and you're going to loop over all the integers,
    it doesn't really matter.

    (the range form creates a list and N integers up front; the xrange form
    creates an iterator object up front and N integers while you're looping.
    what's faster depends on what Python version you're using, and some-
    times also on the phase of the moon)

    in Python 3.0, xrange() will disappear, and range() will return an iterator
    instead.

    </F>
    Fredrik Lundh, Sep 1, 2006
    #1
    1. Advertising

  2. Fredrik Lundh

    Nicko Guest

    Fredrik Lundh wrote:
    > wrote:
    >
    > > I thought the xrange was preferred? for x in xrange(length):

    >
    > preferred by premature optimization freaks, perhaps.


    There's a huge difference between not being profligate with resources
    and premature optimisation. In the case of the idiom "for i in
    range(x):..." there absolutely no utility whatsoever in creating and
    recording the list of objects. Unless it makes a difference to code
    structure or maintainability, I think that not creating stacks of
    objects you don't need is basic code hygiene and not freakish premature
    optimisation.

    > in practice, if the
    > range is reasonably small and you're going to loop over all the integers,
    > it doesn't really matter.


    This is true, but getting into habits that don't matter most of the
    time, but have an performance and stability impact some of the time, is
    worth discouraging.

    > (the range form creates a list and N integers up front; the xrange form
    > creates an iterator object up front and N integers while you're looping.
    > what's faster depends on what Python version you're using, and some-
    > times also on the phase of the moon)


    Using range() is only faster on lists so small then the cost is tiny
    anyway. On any substantial loop it is quite a bit slower and has been
    since python 2.3

    Nicko
    Nicko, Sep 2, 2006
    #2
    1. Advertising

  3. Nicko wrote:

    > There's a huge difference between not being profligate with resources
    > and premature optimisation. In the case of the idiom "for i in
    > range(x):..." there absolutely no utility whatsoever in creating and
    > recording the list of objects. Unless it makes a difference to code
    > structure or maintainability, I think that not creating stacks of
    > objects you don't need is basic code hygiene and not freakish premature
    > optimisation.


    for short lists, both objects create the *same* number of objects.

    if you cannot refrain from pulling arguments out of your ass, you not
    really the right person to talk about hygiene.

    </F>
    Fredrik Lundh, Sep 3, 2006
    #3
  4. Fredrik Lundh

    Nicko Guest

    Fredrik Lundh wrote:
    > Nicko wrote:
    >
    > > ... In the case of the idiom "for i in
    > > range(x):..." there absolutely no utility whatsoever in creating and
    > > recording the list of objects.

    >
    > for short lists, both objects create the *same* number of objects.


    This is true for long lists too, if you iterate over the full range,
    but what I wrote was "creating and recording". The range() function
    generates a variable-sized, potentially large object and retains all of
    the items in the range while xrange() generates a fairly small, fixed
    sized object and only hangs on to one item at a time. Furthermore,
    it's not at all uncommon for loops to be terminated early. With range()
    you incur the cost of creating all the objects, and a list large enough
    to hold them, irrespective of if you are going to use them.

    > if you cannot refrain from pulling arguments out of your ass, you not
    > really the right person to talk about hygiene.


    I'm impressed but your mature argument. Clearly, in the face of such
    compelling reasoning, I shall have to concede that we should all
    generate our range lists up front.

    Nicko
    Nicko, Sep 3, 2006
    #4
  5. Fredrik Lundh

    Steve Holden Guest

    Nicko wrote:
    > Fredrik Lundh wrote:
    >
    >>Nicko wrote:
    >>
    >>
    >>>... In the case of the idiom "for i in
    >>>range(x):..." there absolutely no utility whatsoever in creating and
    >>>recording the list of objects.

    >>
    >>for short lists, both objects create the *same* number of objects.

    >
    >
    > This is true for long lists too, if you iterate over the full range,
    > but what I wrote was "creating and recording". The range() function
    > generates a variable-sized, potentially large object and retains all of
    > the items in the range while xrange() generates a fairly small, fixed
    > sized object and only hangs on to one item at a time. Furthermore,
    > it's not at all uncommon for loops to be terminated early. With range()
    > you incur the cost of creating all the objects, and a list large enough
    > to hold them, irrespective of if you are going to use them.
    >
    >
    >>if you cannot refrain from pulling arguments out of your ass, you not
    >>really the right person to talk about hygiene.

    >
    >
    > I'm impressed but your mature argument. Clearly, in the face of such
    > compelling reasoning, I shall have to concede that we should all
    > generate our range lists up front.
    >

    I'm impressed that you think any of this will be news to the effbot,
    whose sagacity is exceeded only by his irritability in the face of
    ignorance.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
    Steve Holden, Sep 3, 2006
    #5
  6. Fredrik Lundh

    Nicko Guest

    Steve Holden wrote:
    > Nicko wrote:
    > > Fredrik Lundh wrote:
    > >>if you cannot refrain from pulling arguments out of your ass, you not
    > >>really the right person to talk about hygiene.

    > >
    > > I'm impressed but your mature argument. Clearly, in the face of such
    > > compelling reasoning, I shall have to concede that we should all
    > > generate our range lists up front.
    > >

    > I'm impressed that you think any of this will be news to the effbot,
    > whose sagacity is exceeded only by his irritability in the face of
    > ignorance.


    Well, I may not have written as much "award winning" Python software as
    Fredrik, but sagacity usually implies wisdom and good judgement rather
    than mere knowledge. Still I'm hard pressed to see why suggesting
    that, when I want to iterate a number of times, I should use an
    iterator that goes around a number of times (and appreciate the bounded
    storage requirements that result) rather than writing down the list of
    numbers and then selecting each in turn, should warrant such an
    outburst.

    One wonders if the authors of PEP 3100, the outline plans Python 3.0,
    are all premature optimisation freaks too. After all it states that
    sooner or later the built in range function will return an iterator and
    those of us who prefer to iterate rather than count on our
    fingers/lists will have yet another optimisation; we'll not need to put
    an "x" in front of our ranges.

    Nicko
    Nicko, Sep 4, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    0
    Views:
    348
    Xah Lee
    Feb 11, 2005
  2. Putty

    python loops

    Putty, Aug 31, 2006, in forum: Python
    Replies:
    10
    Views:
    833
  3. RE: python loops

    , Aug 31, 2006, in forum: Python
    Replies:
    2
    Views:
    275
    stdazi
    Sep 1, 2006
  4. Wijaya Edward

    Loops Control with Python

    Wijaya Edward, Oct 13, 2006, in forum: Python
    Replies:
    6
    Views:
    259
    Steven Bethard
    Oct 13, 2006
  5. Me
    Replies:
    2
    Views:
    237
Loading...

Share This Page