Note that there are some explanatory texts on larger screens.

plurals
  1. POVariable's memory size in Python
    primarykey
    data
    text
    <p>I am writing Python code to do some big number calculation, and have serious concern about the memory used in the calculation.</p> <p>Thus, I want to count every bit of each variable.</p> <p>For example, I have a variable <strong>x</strong>, which is a big number, and want to count the number of bits for representing <strong>x</strong>.</p> <p>The following code is obviously useless:</p> <pre><code>x=2**1000 len(x) </code></pre> <p>Thus, I turn to use the following code:</p> <pre><code>x=2**1000 len(repr(x)) </code></pre> <p>The variable <strong>x</strong> is (in decimal) is:</p> <blockquote> <p>10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376</p> </blockquote> <p>but the above code returns <strong>303</strong></p> <p>The above long long sequence is of length 302, and so I believe that <strong>303</strong> should be related to the string length only.</p> <p>So, here comes my original question:</p> <p>How can I know the memory size of variable <strong>x</strong>?</p> <p>One more thing; in C/C++ language, if I define</p> <pre><code>int z=1; </code></pre> <p>This means that there are 4 bytes= 32 bits allocated for <strong>z</strong>, and the bits are arranged as 00..001(31 0's and one 1).</p> <p>Here, my variable <strong>x</strong> is huge, I don't know whether it follows the same memory allocation rule? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload