Re: python loops

2006-09-08 Thread Alex Martelli
Tim Roberts [EMAIL PROTECTED] wrote:
   ...
 xrange used to be better.  As I understand it, that's no longer the case.

measuring is better than guessing:

brain:~/codejam alex$ python -V
Python 2.5c1
brain:~/codejam alex$ python -mtimeit 'for x in range(1000):pass'
1 loops, best of 3: 71.9 usec per loop
brain:~/codejam alex$ python -mtimeit 'for x in xrange(1000):pass'
1 loops, best of 3: 54 usec per loop
brain:~/codejam alex$ python -mtimeit -s'y=range(1000)' 'for x in
y:pass'
1 loops, best of 3: 44.8 usec per loop
brain:~/codejam alex$ python -mtimeit -s'y=xrange(1000)' 'for x in
y:pass'
1 loops, best of 3: 53.2 usec per loop
brain:~/codejam alex$ 

So: in 2.5 (2.4 too, do your own timings;-) range is better if you can
create the range once and use it repeatedly, xrange if you have to
create the range each time.  But actually:

brain:~/codejam alex$ python -mtimeit -s'from itertools import repeat'
'for x in repeat(None, 1000): pass'
1 loops, best of 3: 41.3 usec per loop

if you don't actually need the iteration-index in the loop (you just
need to repeat the loop N times), itertools.repeat is even better (well,
if every microsecond counts, anyway;-).


Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-09-08 Thread Jay
Here are my benchmarks for those interested in version 2.4.3

[EMAIL PROTECTED]:~$ python -V
Python 2.4.3
[EMAIL PROTECTED]:~$ python -mtimeit 'for x in range(1000):pass'
1 loops, best of 3: 64.2 usec per loop
[EMAIL PROTECTED]:~$ python -mtimeit 'for x in xrange(1000):pass'
1 loops, best of 3: 50.5 usec per loop
[EMAIL PROTECTED]:~$ python -mtimeit -s'y=range(1000)' 'for x in y:pass'
1 loops, best of 3: 68.2 usec per loop
[EMAIL PROTECTED]:~$ python -mtimeit -s'y=xrange(1000)' 'for x in y:pass'
1 loops, best of 3: 51.4 usec per loop
[EMAIL PROTECTED]:~$ python -mtimeit -s'from itertools import repeat'
'for x in repeat(None, 1000): pass'
1 loops, best of 3: 45.6 usec per loop
[EMAIL PROTECTED]:~$

Alex Martelli wrote:
 Tim Roberts [EMAIL PROTECTED] wrote:
...
  xrange used to be better.  As I understand it, that's no longer the case.

 measuring is better than guessing:

 brain:~/codejam alex$ python -V
 Python 2.5c1
 brain:~/codejam alex$ python -mtimeit 'for x in range(1000):pass'
 1 loops, best of 3: 71.9 usec per loop
 brain:~/codejam alex$ python -mtimeit 'for x in xrange(1000):pass'
 1 loops, best of 3: 54 usec per loop
 brain:~/codejam alex$ python -mtimeit -s'y=range(1000)' 'for x in
 y:pass'
 1 loops, best of 3: 44.8 usec per loop
 brain:~/codejam alex$ python -mtimeit -s'y=xrange(1000)' 'for x in
 y:pass'
 1 loops, best of 3: 53.2 usec per loop
 brain:~/codejam alex$

 So: in 2.5 (2.4 too, do your own timings;-) range is better if you can
 create the range once and use it repeatedly, xrange if you have to
 create the range each time.  But actually:

 brain:~/codejam alex$ python -mtimeit -s'from itertools import repeat'
 'for x in repeat(None, 1000): pass'
 1 loops, best of 3: 41.3 usec per loop

 if you don't actually need the iteration-index in the loop (you just
 need to repeat the loop N times), itertools.repeat is even better (well,
 if every microsecond counts, anyway;-).
 
 
 Alex

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-09-04 Thread Nicko
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-09-03 Thread Tim Roberts
[EMAIL PROTECTED] wrote:

AlbaClause wrote:

 for i in range(length):
 print i

Or usually better:

for ii in xrange(length):
...

xrange used to be better.  As I understand it, that's no longer the case.
-- 
- Tim Roberts, [EMAIL PROTECTED]
  Providenza  Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-09-03 Thread Fredrik Lundh
Tim Roberts wrote:

 xrange used to be better.  As I understand it, that's no longer
  the case.

for short ranges, range is faster in some Python versions, xrange is 
faster in some (including 2.4).  the difference is usually very small 
(the same number of objects are created in both cases).

for long ranges, especially when it's likely that you won't actually 
need all the values, xrange is better.

if you *need* a list, range is better.

in python 3.0, range will return an iterator, and xrange will disappear. 
  if you need a list in 3.0, you will have to do list(range(N)).

/F

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-09-03 Thread Fredrik Lundh
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-09-03 Thread Nicko
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-09-03 Thread Steve Holden
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-09-02 Thread Nicko
Fredrik Lundh wrote:
 [EMAIL PROTECTED] 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-09-01 Thread stdazi
`range' is especially useful for iterating over long sequences ;-)

for i in range(0,100) :
OverflowError: range() result has too many items



Sybren Stuvel wrote:
 [EMAIL PROTECTED] enlightened us with:
  I thought the xrange was preferred?  for x in xrange(length):

 True. It doesn't create the entire list, like range does. range(1000)
 creates a 1000-element list. xrange(1000) just iterates through the
 appropirate values.

  The information contained in this message and any attachment may be
  proprietary, confidential, and privileged or subject to the work
  product doctrine and thus protected from disclosure.  If the reader
  of this message is not the intended recipient, or an employee or
  agent responsible for delivering this message to the intended
  recipient, you are hereby notified that any dissemination,
  distribution or copying of this communication is strictly
  prohibited.  If you have received this communication in error,
  please notify me immediately by replying to this message and
  deleting it and all copies and backups thereof.  Thank you.

 And how are we supposed to interpret this? Copying this communication
 may be prohibited, but both email and usenet messages are copied all
 the time. Without that, both systems fail miserably.

 Sybren
 --
 The problem with the world is stupidity. Not saying there should be a
 capital punishment for stupidity, but why don't we just take the
 safety labels off of everything and let the problem solve itself?
  Frank Zappa

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-09-01 Thread bearophileHUGS
Kay Schluehr:
 I hate ii ;)

It's not nice looking, I agree. A more explicit name is often better.
But I think ii is better than i because you can find it in the code
 (with the Find command of the editor or grep) more easily than i.

Bye,
bearophile

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-09-01 Thread Fredrik Lundh
[EMAIL PROTECTED] 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 



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-09-01 Thread Paddy

Putty wrote:
 In C and C++ and Java, the 'for' statement is a shortcut to make very
 concise loops.  In python, 'for' iterates over elements in a sequence.
 Is there a way to do this in python that's more concise than 'while'?

 C:
 for(i=0; ilength; i++)


 python:
 while i  length:
   i += 1

Someone else gave an apt reply to a similar question before, but I
don't know when so I'll give the gist:
In languages like C and Java, the for loop index variable is often used
to iterate over members of a sequence or other data type.
In Python many of the data types can be directly iterated over without
having to increment a separate indexing value.

C type language:

length = length_finder(some_datatype);
for(i=0; ilength; i++){
  indexed_data = some_datatype[i]
 do_stuff(indexed_data)
}

Becomes the more elegant Python:

for indexed_data in some_datatype:
  do_stuff(indexed_data)


- Paddy.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-08-31 Thread AlbaClause
Putty wrote:

 In C and C++ and Java, the 'for' statement is a shortcut to make very
 concise loops.  In python, 'for' iterates over elements in a sequence.
 Is there a way to do this in python that's more concise than 'while'?
 
 C:
 for(i=0; ilength; i++)
 
 
 python:
 while i  length:
 i += 1

for i in range(length):
print i


-- 
--
There are several things that I will never be:
  *  I will never be attracted to females.
  *  I will never enjoy the company of others.
Exactly how these realities bode for my enemy, is not of my concern.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-08-31 Thread bearophileHUGS
AlbaClause wrote:

 for i in range(length):
 print i

Or usually better:

for ii in xrange(length):
...

Bye,
bearophile

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-08-31 Thread Kay Schluehr
Putty wrote:
 In C and C++ and Java, the 'for' statement is a shortcut to make very
 concise loops.  In python, 'for' iterates over elements in a sequence.
 Is there a way to do this in python that's more concise than 'while'?

 C:
 for(i=0; ilength; i++)


 python:
 while i  length:
   i += 1

As AlbaClause had demonstrated you can iterate over indices as well
using the for-statement and the range() function. But you can also
combine iterating over elements and indices at the same time using the
builtin enumerate() type:

for i, item in enumerate((s1,s2)):
print i,item

0  s1
1  s2

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python loops

2006-08-31 Thread Kay Schluehr

[EMAIL PROTECTED] wrote:
 AlbaClause wrote:

  for i in range(length):
  print i

 Or usually better:
 
 for ii in xrange(length):
   ~~

I hate ii ;)

Regards,
Kay

-- 
http://mail.python.org/mailman/listinfo/python-list


RE: python loops

2006-08-31 Thread Michael . Coll-Barth


 -Original Message-
 From: Kay Schluehr
 
 
  python:
  while i  length:
  i += 1
 
 As AlbaClause had demonstrated you can iterate over indices as well
 using the for-statement and the range() function. But you can also
 combine iterating over elements and indices at the same time using the
 builtin enumerate() type:
 
 for i, item in enumerate((s1,s2)):
 print i,item
 
 0  s1
 1  s2


Newbie here.

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

But, I thought that was too obvious an answer and I was missing something.  I 
tried your code but it would not work.  Then I realized I was behind the times 
with python2.2  It works in, at least, python 2.4


The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure.  If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof.  Thank you.

-- 
http://mail.python.org/mailman/listinfo/python-list