[Numpy-discussion] question about in-place operations
Dag Sverre Seljebotn
d.s.seljebotn at astro.uio.no
Tue May 22 14:47:11 EDT 2012
On 05/22/2012 04:54 PM, Massimo DiPierro wrote:
> For now I will be doing this:
>
> import numpy
> import time
>
> a=numpy.zeros(2000000)
> b=numpy.zeros(2000000)
> c=1.0
>
> # naive solution
> t0 = time.time()
> for i in xrange(len(a)):
> a[i] += c*b[i]
> print time.time()-t0
>
> # possible solution
> n=100000
> t0 = time.time()
> for i in xrange(0,len(a),n):
> a[i:i+n] += c*b[i:i+n]
> print time.time()-t0
>
> the second "possible" solution appears 1000x faster then the former in my tests and uses little extra memory. It is only 2x slower than b*=c.
>
> Any reason not to do it?
No, this is perfectly fine, you just manually did what numexpr does.
On 05/22/2012 04:47 PM, Massimo DiPierro wrote:
> Thank you. I will look into numexpr.
>
> Anyway, I do not need arbitrary expressions. If there were something like
>
> numpy.add_scaled(a,scale,b)
>
> with support for scale in int, float, complex, this would be
sufficient for me.
But of course, few needs *arbitrary* expressions -- it's just that the
ones they want are not already compiled.
It's the last 5% functionality that's different for everybody...
(But the example you mention could make a nice ufunc; so an alternative
for you would be to look at the C implementation of np.add and try to
submit a pull request for numpy.add_scaled)
Dag
More information about the NumPy-Discussion
mailing list