what's the python for this C statement?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Oct 20 18:11:10 EDT 2008


On Mon, 20 Oct 2008 17:01:13 +0000, Lie Ryan wrote:

> On Mon, 20 Oct 2008 12:34:11 +0200, Hrvoje Niksic wrote:
> 
>> Michele <michele at nectarine.it> writes:
>> 
>>> Hi there,
>>> I'm relative new to Python and I discovered that there's one single
>>> way to cycle over an integer variable with for: for i in range(0,10,1)
>> 
>> Please use xrange for this purpose, especially with larger iterations.
>> range actually allocates a sequence.
> 
> Actually that doesn't really matter unless the loop is extremely big (>
> a million), since range is much faster than xrange for smaller values
> (which might be the more typical case). 


That hasn't been true for some time now:

>>> timeit.Timer('range(3)').repeat()
[1.8658630847930908, 0.89470076560974121, 0.88842916488647461]
>>> timeit.Timer('xrange(3)').repeat()
[0.71410012245178223, 0.69949698448181152, 0.69640421867370605]


That's on Python 2.5.



-- 
Steven



More information about the Python-list mailing list