擅长:python、mysql、java
<p>很多。对于简单的内置类型,可以在对象上使用<code>sys.getsizeof</code>来确定与该对象相关联的内存开销(对于容器,这不包括存储在其中的值,只包括用于存储它们的指针)。在</p>
<p>例如,100个smallish <code>int</code>s的<code>list</code>(但要避免小的<code>int</code>缓存大于256)是(在我的3.5.1 Windows x64安装上):</p>
<pre><code>>>> sys.getsizeof([0] * 100) + sys.getsizeof(0) * 100
3264
</code></pre>
<p>或者大约需要3KB的内存。如果这些相同的值存储在<code>numpy</code><code>array</code>的<code>int32</code>中,没有每个数量的Python对象,也没有每个对象的指针,那么大小将下降到大约100*4(再加上几十个字节,对于<code>array</code>对象本身的开销),大约在500字节以下。每增加一个small <code>int</code>的增量成本是对象的24个字节(但是如果它在小int缓存中的值是-5到256个IIRC是免费的),在<code>list</code>中存储8个字节,总共32个字节,而C级类型是4个字节,大约是存储需求的8倍(而且您仍然在存储原始对象)。在</p>
<p>如果你有足够的记忆来处理它,那就随它去吧。但否则,您可能会尝试寻找一个包装,它允许您传入缓冲区协议支持对象(Py3上的<code>numpy.array</code>,<code>array.array</code>,通过memoryview切片分配填充的<code>ctypes</code>数组,等等),因此不需要转换为Python级别的类型。在</p>