-4

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.

7
  • 6
    Allocating and initializing a billion numbers takes time.
    – Barmar
    Commented Jan 26, 2023 at 20:32
  • 1
    One Python integer is 32 bits, so a tuple of 10^9 elements is about 30GB in size. I think it's no wonder why your laptop freezes. Commented Jan 26, 2023 at 20:32
  • 2
    Is this a joke question? It's a little early for Apr 1
    – wim
    Commented Jan 26, 2023 at 20:33
  • 1
    This classic video shows how small changes in an exponent corresponds to huge changes in size. Commented Jan 26, 2023 at 20:39
  • 1
    Perhaps there is a way to get what you seek a different way. What is the motivation for the creation of a billion element tuple?
    – JonSG
    Commented Jan 26, 2023 at 20:42

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.