numpy.cross and similiar functions: Do they allocate a new array on every call?
<p>When I use <code>numpy.cross</code>, it will <em>return</em> an array with the results. There's no way to compute <em>into</em> an existing array. The same holds for other functions.</p>

<ol>
<li>Isn't it extremely inefficient to allocate a new array upon each call?</li>
<li>If so, is there a way to speed it up?</li>
</ol>

<p>There is an overhead with the function <code>np.cross</code> as it creates a new NumPy array. You can do <code>x = np.cross(x, y)</code> but it will not suppress the overhead.</p>

<p>If you have a program where this is actually a problem (as diagnosed by profiling the program, for instance), you are better off turning to a specific optimization strategy. <a href="http://cython.org/" rel="nofollow">Cython</a> and <a href="http://numba.pydata.org/" rel="nofollow">Numba</a> come to mind.</p>

python numpy