Search code examples
pythontuplesrangeexponent

Why does this python statement got stuck?


Have anyone ever experienced this statement? The following code looks work well, but it makes my laptop get stuck when the exponent reaches 9 or above.

ordered_tuple = tuple(range(10**9))

Every time I run this statement, my laptop slows down, got stuck taking up 100% of RAM usage. I searched for the reason why this happened, but there was no suitable answer.

Please help me understand why this code makes computers busy. Thanks in advance.

I tried with different exponent between [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 1-8 works fairly well as I expected.

ordered_tuple = tuple(range(10**1))
ordered_tuple = tuple(range(10**2))
ordered_tuple = tuple(range(10**3))
ordered_tuple = tuple(range(10**4))
ordered_tuple = tuple(range(10**5))
ordered_tuple = tuple(range(10**6))
ordered_tuple = tuple(range(10**7))
ordered_tuple = tuple(range(10**8))

whereas 9, 10 doesn't work.


Solution

  • 10**9 is 10 times as big as 10**8. 10**10 is 100 times as big as 10**8. Likely your computer just does not have enough RAM to hold all these numbers.

    PS: Not sure how long is a Python int, if it is 4-byte long, then 10**9 of them is 4 GB, but then 10**10 does not fit in 4-byte long int and needs more. If it is 8-byte long, then 10**9 is 8 GB and 10**10 is 80 GB.