l = range(int(1E9))
BartC
bc at freeuk.com
Sat May 2 11:26:40 EDT 2015
On 30/04/2015 18:20, Ben Finney wrote:
> Jon Ribbens <jon+usenet at unequivocal.co.uk> writes:
>> If you use xrange() instead of range() then you will get an iterator
>> which will return each of the numbers in turn without any need to
>> create an enormous list of all of them.
>
> If you use Python 3 instead of the obsolescent Python 2, the ‘range’
> callable has this sensible behaviour by default.
When I first looked at Python 20 or so years ago this seemed to be the
standard way of writing a for-loop:
for i in range(N):
....
I remember being completely astonished at the time that 'range' actually
created a list of values from 0 to N-1.
Python was already known to be slow yet it deliberately crippled itself
by using just about the slowest method imaginable of executing a simple
iterative loop? By first creating a list of a million objects!
And sometimes you weren't even interested in the values but just wanted
to execute something N times so it was a wasted effort.
That was eventually fixed with xrange, but why do it like that in the
first place?
(At the time, I was creating bytecode languages as part of the
applications I was writing. An empty for-loop executed just one bytecode
per iteration, and it was also the fastest bytecode instruction. An
empty for-loop executed three Python bytecodes per iteration last time I
looked. It seems that Python used to like making a rod for its own back.)
--
Bartc
More information about the Python-list
mailing list