How much overhead does python numpy tolist() add?
<p>I am using a python program that uses numpy array as standard data type for arrays. For the heavy computation I pass the arrays to a C++ library. In order to do so, I use <a href="https://github.com/pybind/pybind11" rel="nofollow">pybind</a>. However, I am required to use python <code>list</code>. I do the conversion from <code>numpy</code> array and <code>list</code> via:</p>

<pre><code>NativeSolver.vector_add(array1.tolist(), array2.tolist(), ...)
</code></pre>

<p>How much overhead does this conversion generate? I hope it doesn't create a whole new copy. Numpy reference says:</p>

<blockquote>
  <p><strong>ndarray.tolist()</strong></p>
  
  <p>Return a copy of the array data as a (nested) Python list. Data items
  are converted to the nearest compatible Python type.</p>
</blockquote>

<p>A lot. For simple built-in types, you can use <code>sys.getsizeof</code> on an object to determine the memory overhead associated with that object (for containers, this does not include the values stored in it, only the pointers used to store them).</p>

<p>So for example, a <code>list</code> of 100 smallish <code>int</code>s (but greater than 256 to avoid small <code>int</code> cache) is  (on my 3.5.1 Windows x64 install):</p>

<pre><code>&gt;&gt;&gt; sys.getsizeof([0] * 100) + sys.getsizeof(0) * 100
3264
</code></pre>

<p>or about 3 KB of memory required. If those same values were stored in a <code>numpy</code> <code>array</code> of <code>int32</code>s, with no Python objects per number, and no per object pointers, the size would drop to roughly 100 * 4 (plus another few dozen bytes, for the <code>array</code> object overhead itself), somewhere under 500 bytes. The incremental cost for each additional small <code>int</code> is 24 bytes for the object (though it's free if it's in the small int cache for values from -5 to 256 IIRC), and 8 bytes for the storage in the <code>list</code>, 32 bytes total, vs. 4 for the C level type, roughly 8x the storage requirements (and you're still storing the original object too).</p>

<p>If you have enough memory to deal with it, so be it. But otherwise, you might trying looking at a wrapping that lets you pass in buffer protocol supporting objects (<code>numpy.array</code>, <code>array.array</code> on Py3, <code>ctypes</code> arrays populated via memoryview slice assignment, etc.) so conversion to Python level types isn't needed.</p>

python c++ arrays numpy wrapper