l = range(int(1E9))

Marko Rauhamaa marko at pacujo.net
Sat May 2 15:15:49 EDT 2015


BartC <bc at freeuk.com>:
> (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!)

You're doing it wrong. Here's the Pythonic way to iterate over a billion
numbers:
 def loop(i):
 if i < 1000000000:
 do_stuff(i)
 keep_iterating(i + 1)
 loop(0)
Marko


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /