l = range(int(1E9))
BartC
bc at freeuk.com
Sat May 2 14:34:41 EDT 2015
On 02/05/2015 17:39, Mark Lawrence wrote:
> On 02/05/2015 17:17, BartC wrote:
>> On 02/05/2015 16:40, Mark Lawrence wrote:
>>> for item in items:
>>>
>>> When did this change, or has it always been this way and you were simply
>>> using an idiom from other languages?
>>
>> Your example is the equivalent of 'forall' in other languages, where you
>> iterate over the values of some collection of data.
>>
>> I agree that most for-loops in Pythonic code probably fall into that
>> category.
>>
>> But for looping over a simple integer range, then using 'range' to
>> denote the range (and build a list as it used to do), was how it was
>> done. And earlier on people would have been porting coding code to
>> Python at which point a straightforward 'for i=a to b' loop suddenly
>> acquired a substantial overhead it didn't have before!
>>
>
> All you are saying is that they didn't bother reading the docs and
> learning how to write a *PYTHON* for loop. Failing that don't bother
> using range, just directly convert your (say) C loop into Python. I
> really don't see any issue here at all.
OK, so it's the programmer's fault if as fundamental a concept as a
for-loop ranging over integers is implemented inefficiently. He has to
transform it into high-level terms, or has to reconstruct it somehow
using a while-loop and an incrementing loop index.
Now I understand (why Python has been beset for so long with performance
problems, if that is a typical attitude!).
BTW, why did they introduce the 'xrange' thing; for fun? Or did someone
realise there might have been an issue after all?
(I tried an empty loop counting to 1 billion in Python 2.x, using 'for i
in range'. It ran out of memory. Counting to 100 million instead, it
worked, but still used a massive 1.5GB RAM while doing so (and took 6
seconds to count to 100M, not too bad for Python)
Outside Python, it might typically take a few seconds to count to 1
billion, and would use virtually no memory.
Why would anyone want to loop over all those numbers instead of
iterating over actual data? For any number of reasons. Counting how many
happy numbers are in that range for one!)
--
Bartc
More information about the Python-list
mailing list