How much RAM do I need to execute x=10**10**10 in Python?
Question:
By timing the execution of x=10**10**n
for n up to 8, I estimate that about 4.3 days would be needed to execute x=10**10**10
on my system. What I need to know, however, is whether I would have enough RAM. (My 64-bit Windows system has 16GB of RAM, 10.5GB free at the beginning of execution.)
Is it correct that executing x=10**10**10
requires about log_2(10**10**10)
bits, i.e. about 33*10**9
bits, or about 4.1 GB of RAM?
Is there some Python or system function that will report the amount of RAM used by x=10**10**n
for n up to 8, so I could estimate the amount needed for n=10 (similar to what I did for the execution times)?
Answers:
There’s a bit of overhead in the standard Python integer representation; it only uses 30 bits of each 32-bit word. So your memory estimate is a bit low; you would get a better estimate by multiplying by 32.0/30. But you have enough memory if a single Python process is allowed to use that much on your system.
You can get a more precise measure of the size of a primitive object using sys.getsizeof
, which shows that the estimate can be made pretty close:
>>> import sys.getsizeof
>>> getsizeof(10**10**7)
4429264
>>> log2(10) * 10**7
33219280.94887362
>>> log2(10) * 10**7 * (32/30) / 8
4429237.459849816
By the way, for calculations involving numbers this size, you’ll find that the decimal
module is a lot faster. But you have to modify the decimal "context" to allow numbers of arbitrary precision; instructions can be found at the end of the FAQ in the module documentation
By timing the execution of x=10**10**n
for n up to 8, I estimate that about 4.3 days would be needed to execute x=10**10**10
on my system. What I need to know, however, is whether I would have enough RAM. (My 64-bit Windows system has 16GB of RAM, 10.5GB free at the beginning of execution.)
Is it correct that executing x=10**10**10
requires about log_2(10**10**10)
bits, i.e. about 33*10**9
bits, or about 4.1 GB of RAM?
Is there some Python or system function that will report the amount of RAM used by x=10**10**n
for n up to 8, so I could estimate the amount needed for n=10 (similar to what I did for the execution times)?
There’s a bit of overhead in the standard Python integer representation; it only uses 30 bits of each 32-bit word. So your memory estimate is a bit low; you would get a better estimate by multiplying by 32.0/30. But you have enough memory if a single Python process is allowed to use that much on your system.
You can get a more precise measure of the size of a primitive object using sys.getsizeof
, which shows that the estimate can be made pretty close:
>>> import sys.getsizeof
>>> getsizeof(10**10**7)
4429264
>>> log2(10) * 10**7
33219280.94887362
>>> log2(10) * 10**7 * (32/30) / 8
4429237.459849816
By the way, for calculations involving numbers this size, you’ll find that the decimal
module is a lot faster. But you have to modify the decimal "context" to allow numbers of arbitrary precision; instructions can be found at the end of the FAQ in the module documentation