0

I'm sure this question has been asked before but I couldn't find it.

As part of a pretty simple programming problem in a homework assignment, I've been told I need to loop 10^8 times. But any time I try to loop that many times my program (and whole computer) hangs.

Since this is for a homework assignment, I won't share my actual code, but even this really basic script hangs on my computer:

for i in range(10**8):
... j = i

10^7 loops works fine. 10^8 always hangs.

I'm not a new python user but I'm not sure how to diagnose this. Is it a memory-related issue? What are some strategies for fixing that? Is it just a limit in Python?

I'm using Python 2.7 if that's relevant.

(Also, for the record, the homework assignment has nothing to do with solving this problem. N=10^8 is just what my professor randomly chose.)

8
  • 1
    10^8 == 2, so that really shouldn't hang. Nov 13, 2015 at 3:20
  • Another good link here for a Python 3 perspective. Nov 13, 2015 at 3:22
  • Briefly, yes, it is a memory issue. Python 2's range() will create an entire list, storing all those numbers in memory at the same time. As soon as your computer runs out of RAM and starts into virtual memory (HDD), that'll cause a dramatic slowdown. Nov 13, 2015 at 3:23
  • @TigerhawkT3 Thanks! That's useful.
    – s.py
    Nov 13, 2015 at 3:26
  • I really don't understand why new Python students are being taught Python 2. Nov 13, 2015 at 3:26

0

Browse other questions tagged or ask your own question.