[Tutor] Efficiency of while versus (x)range
Corey Richardson
kb1pkl at aim.com
Thu Mar 17 02:33:03 CET 2011
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
More information about the Tutor
mailing list