<div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">(disclosure: I have been a long time maintainer of numexpr)</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">An important thing to be noted is that, besides avoiding creating big temporaries, numexpr has support for multi-threading out of the box and Intel VML for optimal evaluation times on Intel CPUs. For choosing your best bet, there is no replacement for experimentation:</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><a href="https://gist.github.com/FrancescAlted/203be8a44d02566f31dae11a22c179f3">https://gist.github.com/FrancescAlted/203be8a44d02566f31dae11a22c179f3</a><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">I have no time now to check for memory consumption, but you can expect numexpr and Numba consuming barely the same amount of memory. Performance wise things are quite different, but this is probably due to my inexperience with Numba (in particular, paralellism does not seem to work for this example in Numba 0.45, but I am not sure why).</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">Cheers!</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"> Probably numba has this too, but my attempts to use parallelism failed</div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Missatge de Sebastian Berg <<a href="mailto:sebastian@sipsolutions.net">sebastian@sipsolutions.net</a>> del dia dc., 24 de jul. 2019 a les 1:32:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">On Tue, 2019-07-23 at 13:38 -0500, Stanley Seibert wrote:<br>
> (Full disclosure: I work on Numba...) <br>
> <br>
> Just to note, the NumPy implementation will allocate (and free) more<br>
> than 2 arrays to compute that expression. It has to allocate the<br>
> result array for each operation as Python executes. That expression<br>
> is equivalent to:<br>
> <br>
<br>
That is mostly true, although – as Hameer mentioned – on many platforms<br>
(gcc compiler is needed I think) a bit of magic happens.<br>
<br>
If an array is temporary, the operation is replaced with an in-place<br>
operation for most python operators calls. For example:<br>
`-abs(arr1 * arr2 / arr3 - arr4)`<br>
should only create a single new array and keep reusing it in many<br>
cases[0]. You would achieve similar things with `arr1 *= arr2`<br>
manually.<br>
<br>
Another thing is that numpy will cache some arrays, so that the<br>
allocation cost itself may be avoided in many cases.<br>
<br>
NumPy does no "loop fusing", i.e. each operation is finished before the<br>
next is started. In many cases, with simple math loop fusing can give a<br>
very good speedup (which is wher Numba or numexpr come in). Larger<br>
speedups are likely if you have large arrays and very simple math<br>
(addition). [1]<br>
<br>
As Stanley noted, you probably should not worry too much about it. You<br>
have `exp`/`sin` in there, which are slow by nature. You can try, but<br>
it is likely that you simply cannot gain much speed there.<br>
<br>
Best,<br>
<br>
Sebastian<br>
<br>
<br>
[0] It requires that the shapes all match and that the result arrays<br>
are obviously temporary.<br>
[1] For small arrays overheads may be avoided using tools such as<br>
numba, which can help a lot as well. If you want to use multiple<br>
threads for a specific function that may also be worth a look.<br>
<br>
> s1 = newfactor * x<br>
> s2 = np.exp(s1)<br>
> s3 = np.sin(x)<br>
> y = s3 * s2<br>
> <br>
> However, memory allocation is still pretty fast compared to special<br>
> math functions (exp and sin), which dominate that calculation. I<br>
> find this expression takes around 20 milliseconds for a million<br>
> elements on my older laptop, so that might be negligible in your<br>
> program execution time unless you need to recreate this decaying<br>
> exponential thousands of times. Tools like Numba or numexpr will be<br>
> useful to fuse loops so you only do one allocation, but they aren't<br>
> necessary unless this becomes the bottleneck in your code.<br>
> <br>
> If you are getting started with NumPy, I would suggest not worrying<br>
> about these issues too much, and focus on making good use of arrays,<br>
> NumPy array functions, and array expressions in your code. If you<br>
> have to write for loops (if there is no good way to do the operation<br>
> with existing NumPy functions), I would reach for something like<br>
> Numba, and if you want to speed up complex array expressions, both<br>
> Numba and Numexpr will do a good job.<br>
> <br>
> <br>
> On Tue, Jul 23, 2019 at 10:38 AM Hameer Abbasi <<br>
> <a href="mailto:einstein.edison@gmail.com" target="_blank">einstein.edison@gmail.com</a>> wrote:<br>
> > Hi Ram,<br>
> > <br>
> > No, NumPy doesn’t have a way. And it newer versions, it probably<br>
> > won’t create two arrays if all the dtypes match, it’ll do some<br>
> > magic to re use the existing ones, although it will use multiple<br>
> > loops instead of just one.<br>
> > <br>
> > You might want to look into NumExpr or Numba if you want an<br>
> > efficient implementation.<br>
> > <br>
> > Get Outlook for iOS<br>
> > <br>
> > From: NumPy-Discussion <<br>
> > numpy-discussion-bounces+einstein.edison=<a href="mailto:gmail.com@python.org" target="_blank">gmail.com@python.org</a>> on<br>
> > behalf of Ram Rachum <<a href="mailto:ram@rachum.com" target="_blank">ram@rachum.com</a>><br>
> > Sent: Tuesday, July 23, 2019 7:29 pm<br>
> > To: <a href="mailto:numpy-discussion@python.org" target="_blank">numpy-discussion@python.org</a><br>
> > Subject: [Numpy-discussion] Creating a sine wave with exponential<br>
> > decay<br>
> > <br>
> > Hi everyone! Total Numpy newbie here.<br>
> > <br>
> > I'd like to create an array with a million numbers, that has a sine<br>
> > wave with exponential decay on the amplitude.<br>
> > <br>
> > In other words, I want the value of each cell n to be sin(n)<br>
> > * 2 ** (-n * factor).<br>
> > <br>
> > What would be the most efficient way to do that?<br>
> > <br>
> > Someone suggested I do something like this: <br>
> > <br>
> > y = np.sin(x) * np.exp(newfactor * x)<br>
> > But this would create 2 arrays, wouldn't it? Isn't that wasteful?<br>
> > Does Numpy provide an efficient way of doing that without creating<br>
> > a redundant array?<br>
> > <br>
> > <br>
> > <br>
> > Thanks for your help,<br>
> > <br>
> > Ram Rachum.<br>
> > <br>
> > _______________________________________________<br>
> > NumPy-Discussion mailing list<br>
> > <a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
> > <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
> <br>
> _______________________________________________<br>
> NumPy-Discussion mailing list<br>
> <a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature">Francesc Alted</div>