<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On 12 October 2017 at 15:18, Ant <span dir="ltr"><<a href="mailto:antoniy.py@gmail.com" target="_blank">antoniy.py@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">



<div>
<div name="messageBodySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">@Daπid
<div>Thank you! This indeed is twice as fast as doing it normally (not counting the fixed time of sorting A, of course). </div>
<div>I would still like to speed it up, getting another 2x  speedup. This is my code, please tell me if you have any suggestions!</div></div></div></blockquote><div><br></div><div><br></div><div>As always, when optimising you must profile. For A of size 3000000 and k of size 30, this is what I get:</div><div><br></div><div><a href="https://gist.github.com/Dapid/ed23a1bb8e96782c0b698edecff14435">https://gist.github.com/Dapid/ed23a1bb8e96782c0b698edecff14435</a></div><div><br></div><div>89% of the time is being spent on this line (and it gets worse as you increase the size of A):</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div name="messageBodySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif"><div><pre></pre>
<pre style="font-family:Menlo;font-size:10.5pt"><span style="font-size:10.5pt">indicesClosest = <font color="#000080">final_indices[inv_idx_sort]</font></span></pre>
<pre></pre>
</div></div></div></blockquote><div><br></div><div>I don't know from the top of my head of a faster way of doing this, so, can you somehow adapt your problem to use your sorted indexes of A instead? This you can very easily rewrite unrolled in Cython, I think you can scrape a bit of time there.</div><div><br></div><div>Here is a good tutorial for Numpy: <a href="http://docs.cython.org/en/latest/src/userguide/numpy_tutorial.html">http://docs.cython.org/en/latest/src/userguide/numpy_tutorial.html</a><br></div></div></div></div>