<div dir="ltr"><div>For one thing, `C = np.empty(shape=(n,n), dtype='float64')` allocates 10^4 extra elements before being immediately discarded.<br><br></div>    -Joe<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Feb 28, 2017 at 5:57 PM, Sebastian K <span dir="ltr"><<a href="mailto:sebastiankaster@googlemail.com" target="_blank">sebastiankaster@googlemail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Yes it is true the execution time is much faster with the numpy function. <div><br></div><div> The Code for numpy version:</div><div><br></div><div><div>def createMatrix(n):</div><div><span class="m_-4246596707740004624gmail-Apple-tab-span" style="white-space:pre-wrap">      </span>Matrix = np.empty(shape=(n,n), dtype='float64')</div><div><span class="m_-4246596707740004624gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>for x in range(n):</div><div><span class="m_-4246596707740004624gmail-Apple-tab-span" style="white-space:pre-wrap">          </span>for y in range(n):</div><div><span class="m_-4246596707740004624gmail-Apple-tab-span" style="white-space:pre-wrap">                  </span>Matrix[x, y] = 0.1 + ((x*y)%1000)/1000.0</div><div><span class="m_-4246596707740004624gmail-Apple-tab-span" style="white-space:pre-wrap">    </span>return Matrix </div><div><br></div><div><br></div><div><br></div><div>if __name__ == '__main__':</div><div><span class="m_-4246596707740004624gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>n = getDimension()</div><div><span class="m_-4246596707740004624gmail-Apple-tab-span" style="white-space:pre-wrap">  </span>if n > 0:</div><div><span class="m_-4246596707740004624gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>A = createMatrix(n)</div><div><span class="m_-4246596707740004624gmail-Apple-tab-span" style="white-space:pre-wrap">         </span>B = createMatrix(n)</div><div><span class="m_-4246596707740004624gmail-Apple-tab-span" style="white-space:pre-wrap">         </span>C = np.empty(shape=(n,n), dtype='float64')</div><div><span class="m_-4246596707740004624gmail-Apple-tab-span" style="white-space:pre-wrap">          </span>C = np.dot(A,B)</div><div><br></div><div><span class="m_-4246596707740004624gmail-Apple-tab-span" style="white-space:pre-wrap">            </span>#print(C)</div><div><br></div></div><div>In the pure python version I am just implementing the multiplication with three for-loops.</div><div><br></div><div>Measured data with libmemusage:</div><div><div>dimension of matrix: 100x100</div><div>heap peak pure python3: 1060565</div></div><div>heap peakt numpy function: 4917180</div><div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">2017-02-28 23:17 GMT+01:00 Matthew Brett <span dir="ltr"><<a href="mailto:matthew.brett@gmail.com" target="_blank">matthew.brett@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<span><br>
On Tue, Feb 28, 2017 at 2:12 PM, Sebastian K<br>
<<a href="mailto:sebastiankaster@googlemail.com" target="_blank">sebastiankaster@googlemail.co<wbr>m</a>> wrote:<br>
> Thank you for your answer.<br>
> For example a very simple algorithm is a matrix multiplication. I can see<br>
> that the heap peak is much higher for the numpy version in comparison to a<br>
> pure python 3 implementation.<br>
> The heap is measured with the libmemusage from libc:<br>
><br>
>           heap peak<br>
>                   Maximum of all size arguments of malloc(3), all products<br>
>                   of nmemb*size of calloc(3), all size arguments of<br>
>                   realloc(3), length arguments of mmap(2), and new_size<br>
>                   arguments of mremap(2).<br>
<br>
</span>Could you post the exact code you're comparing?<br>
<br>
I think you'll find that a naive Python 3 matrix multiplication method<br>
is much, much slower than the same thing with Numpy, with arrays of<br>
any reasonable size.<br>
<br>
Cheers,<br>
<br>
Matthew<br>
<div class="m_-4246596707740004624HOEnZb"><div class="m_-4246596707740004624h5">______________________________<wbr>_________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br>
<a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman<wbr>/listinfo/numpy-discussion</a><br>
</div></div></blockquote></div><br></div>
</div></div><br>______________________________<wbr>_________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/<wbr>mailman/listinfo/numpy-<wbr>discussion</a><br>
<br></blockquote></div><br></div>