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.)

s.py
  • 187
  • 10
  • 1
    `10^8 == 2`, so that really shouldn't hang. – user2357112 supports Monica Nov 13 '15 at 03:20
  • Another good link [here](http://stackoverflow.com/questions/30081275/why-is-1000000000000000-in-range1000000000000001-so-fast-in-python-3) for a Python 3 perspective. – TigerhawkT3 Nov 13 '15 at 03: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. – TigerhawkT3 Nov 13 '15 at 03:23
  • @TigerhawkT3 Thanks! That's useful. – s.py Nov 13 '15 at 03:26
  • I really don't understand why new Python students are being taught Python 2. – TigerhawkT3 Nov 13 '15 at 03:26
  • I'm actually not a new Python student. Just using python for an assignment that did not specify language. Have 2 and 3 installed and went with 2 for no particular reason. (My school doesn't care to teach any version of python for some reason) – s.py Nov 13 '15 at 03:28
  • Ah, I see. Anyway, I'd recommend sticking with 3 when you can, for reasons like this. – TigerhawkT3 Nov 13 '15 at 03:29
  • Sounds good, thanks. – s.py Nov 13 '15 at 03:30

0 Answers0