<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi Francesc,<div class=""><br class=""></div><div class="">Those numbers are really eye-popping! But the formatting of the code as a string still bugs me a lot. Asking this as a totally naive user, do you know whether <a href="https://www.python.org/dev/peps/pep-0523/" class="">PEP523</a> (adding a frame evaluation API) would allow numexpr to have a more Pythonic syntax? eg. </div><div class=""><br class=""></div><div class="">with numexpr:</div><div class="">    y = np.sin(x) * np.exp(newfactor * x)</div><div class=""><br class=""></div><div class="">?</div><div class=""><br class=""></div><div class="">Juan.</div><div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 24 Jul 2019, at 6:39 pm, Francesc Alted <<a href="mailto:faltet@gmail.com" class="">faltet@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div dir="ltr" class=""><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 class=""></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 class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><a href="https://gist.github.com/FrancescAlted/203be8a44d02566f31dae11a22c179f3" class="">https://gist.github.com/FrancescAlted/203be8a44d02566f31dae11a22c179f3</a><br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br class=""></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 class=""></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 class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br class=""></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 class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Missatge de Sebastian Berg <<a href="mailto:sebastian@sipsolutions.net" class="">sebastian@sipsolutions.net</a>> del dia dc., 24 de jul. 2019 a les 1:32:<br class=""></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 class="">
> (Full disclosure: I work on Numba...) <br class="">
> <br class="">
> Just to note, the NumPy implementation will allocate (and free) more<br class="">
> than 2 arrays to compute that expression.  It has to allocate the<br class="">
> result array for each operation as Python executes.  That expression<br class="">
> is equivalent to:<br class="">
> <br class="">
<br class="">
That is mostly true, although – as Hameer mentioned – on many platforms<br class="">
(gcc compiler is needed I think) a bit of magic happens.<br class="">
<br class="">
If an array is temporary, the operation is replaced with an in-place<br class="">
operation for most python operators calls. For example:<br class="">
`-abs(arr1 * arr2 / arr3 - arr4)`<br class="">
should only create a single new array and keep reusing it in many<br class="">
cases[0]. You would achieve similar things with `arr1 *= arr2`<br class="">
manually.<br class="">
<br class="">
Another thing is that numpy will cache some arrays, so that the<br class="">
allocation cost itself may be avoided in many cases.<br class="">
<br class="">
NumPy does no "loop fusing", i.e. each operation is finished before the<br class="">
next is started. In many cases, with simple math loop fusing can give a<br class="">
very good speedup (which is wher Numba or numexpr come in). Larger<br class="">
speedups are likely if you have large arrays and very simple math<br class="">
(addition). [1]<br class="">
<br class="">
As Stanley noted, you probably should not worry too much about it. You<br class="">
have `exp`/`sin` in there, which are slow by nature. You can try, but<br class="">
it is likely that you simply cannot gain much speed there.<br class="">
<br class="">
Best,<br class="">
<br class="">
Sebastian<br class="">
<br class="">
<br class="">
[0] It requires that the shapes all match and that the result arrays<br class="">
are obviously temporary.<br class="">
[1] For small arrays overheads may be avoided using tools such as<br class="">
numba, which can help a lot as well. If you want to use multiple<br class="">
threads for a specific function that may also be worth a look.<br class="">
<br class="">
> s1 = newfactor * x<br class="">
> s2 = np.exp(s1)<br class="">
> s3 = np.sin(x)<br class="">
> y = s3 * s2<br class="">
> <br class="">
> However, memory allocation is still pretty fast compared to special<br class="">
> math functions (exp and sin), which dominate that calculation.  I<br class="">
> find this expression takes around 20 milliseconds for a million<br class="">
> elements on my older laptop, so that might be negligible in your<br class="">
> program execution time unless you need to recreate this decaying<br class="">
> exponential thousands of times.  Tools like Numba or numexpr will be<br class="">
> useful to fuse loops so you only do one allocation, but they aren't<br class="">
> necessary unless this becomes the bottleneck in your code.<br class="">
> <br class="">
> If you are getting started with NumPy, I would suggest not worrying<br class="">
> about these issues too much, and focus on making good use of arrays,<br class="">
> NumPy array functions, and array expressions in your code.  If you<br class="">
> have to write for loops (if there is no good way to do the operation<br class="">
> with existing NumPy functions), I would reach for something like<br class="">
> Numba, and if you want to speed up complex array expressions, both<br class="">
> Numba and Numexpr will do a good job.<br class="">
> <br class="">
> <br class="">
> On Tue, Jul 23, 2019 at 10:38 AM Hameer Abbasi <<br class="">
> <a href="mailto:einstein.edison@gmail.com" target="_blank" class="">einstein.edison@gmail.com</a>> wrote:<br class="">
> > Hi Ram,<br class="">
> > <br class="">
> > No, NumPy doesn’t have a way. And it newer versions, it probably<br class="">
> > won’t create two arrays if all the dtypes match, it’ll do some<br class="">
> > magic to re use the existing ones, although it will use multiple<br class="">
> > loops instead of just one.<br class="">
> > <br class="">
> > You might want to look into NumExpr or Numba if you want an<br class="">
> > efficient implementation.<br class="">
> > <br class="">
> > Get Outlook for iOS<br class="">
> >  <br class="">
> > From: NumPy-Discussion <<br class="">
> > numpy-discussion-bounces+einstein.edison=<a href="mailto:gmail.com@python.org" target="_blank" class="">gmail.com@python.org</a>> on<br class="">
> > behalf of Ram Rachum <<a href="mailto:ram@rachum.com" target="_blank" class="">ram@rachum.com</a>><br class="">
> > Sent: Tuesday, July 23, 2019 7:29 pm<br class="">
> > To: <a href="mailto:numpy-discussion@python.org" target="_blank" class="">numpy-discussion@python.org</a><br class="">
> > Subject: [Numpy-discussion] Creating a sine wave with exponential<br class="">
> > decay<br class="">
> >  <br class="">
> > Hi everyone! Total Numpy newbie here.<br class="">
> > <br class="">
> > I'd like to create an array with a million numbers, that has a sine<br class="">
> > wave with exponential decay on the amplitude.<br class="">
> > <br class="">
> > In other words, I want the value of each cell n to be sin(n)<br class="">
> >  * 2 ** (-n * factor).<br class="">
> > <br class="">
> > What would be the most efficient way to do that?<br class="">
> > <br class="">
> > Someone suggested I do something like this: <br class="">
> > <br class="">
> > y = np.sin(x) * np.exp(newfactor * x)<br class="">
> > But this would create 2 arrays, wouldn't it? Isn't that wasteful?<br class="">
> > Does Numpy provide an efficient way of doing that without creating<br class="">
> > a redundant array?<br class="">
> > <br class="">
> > <br class="">
> > <br class="">
> > Thanks for your help,<br class="">
> > <br class="">
> > Ram Rachum.<br class="">
> > <br class="">
> > _______________________________________________<br class="">
> > NumPy-Discussion mailing list<br class="">
> > <a href="mailto:NumPy-Discussion@python.org" target="_blank" class="">NumPy-Discussion@python.org</a><br class="">
> > <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank" class="">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br class="">
> <br class="">
> _______________________________________________<br class="">
> NumPy-Discussion mailing list<br class="">
> <a href="mailto:NumPy-Discussion@python.org" target="_blank" class="">NumPy-Discussion@python.org</a><br class="">
> <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank" class="">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br class="">
_______________________________________________<br class="">
NumPy-Discussion mailing list<br class="">
<a href="mailto:NumPy-Discussion@python.org" target="_blank" class="">NumPy-Discussion@python.org</a><br class="">
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank" class="">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br class="">
</blockquote></div><br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div dir="ltr" class="gmail_signature">Francesc Alted</div>
_______________________________________________<br class="">NumPy-Discussion mailing list<br class=""><a href="mailto:NumPy-Discussion@python.org" class="">NumPy-Discussion@python.org</a><br class="">https://mail.python.org/mailman/listinfo/numpy-discussion<br class=""></div></blockquote></div><br class=""></div></body></html>