On 03/16/2011 08:32 PM, Shane O'Connor wrote:
> Hi,
> 
> First-time poster here. I've a question about loop efficiency - I was
> wondering whether this code:
> 
> i = 0
> while i < 1000:
>     do something
>     i+=1
> 
> is more efficient than:
> 
> for i in range(1000):
>     do something
> 
> or:
> 
> for i in xrange(1000):
>     do something
> 
> In my mind, the while loop should not allocate as much memory as range or
> have the overhead of the iterator of xrange (although aesthetically, I
> prefer the x/range style). Is this the case or does the compiler do
> something clever here?
> 

Only way to know is to check!
http://docs.python.org/library/timeit.html

--
Corey Richardson
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to