[Numpy-discussion] Enhancing dot()
tim.hochberg at cox.net
Fri Jul 7 01:53:29 CDT 2006
Bill Baxter wrote:
> On 7/7/06, *Tim Hochberg* <tim.hochberg at cox.net
> <mailto:tim.hochberg at cox.net>> wrote:
> > The funny thing is that having a dot(a,b,c,...) would lead to the
> > exact same kind of hidden performance problems you're arguing
> Not exactly arguing -- this isn't why I don't like H and friends
> -- just
> noting that this is one of the traps that people are likely to
> fall into
> when transferring equations to code.
> There's a strong argument to be made that the whole design of most
> array math packages is flawed and leads to inefficient code. The
> classic example is something like:
> A = B + C - 2*D
> where all the matrices are 2million x 2million. I think for numpy
> that would basically do:
> tmp = B+C
> tmp2 = 2*D
> tmp3 = tmp - tmp2
> A = tmp3
> Allocating three huge tmp variables and probably doing an extra copy
> or two in there, when the best thing to do would be more like:
> A = D
> A *= -2
> A += C
> A += B
> Or something like that.
Yeah -- that's not quite right since you've clobbered B. But that is
essentially the dark road that people go down in performance critical
sections on occasion.
> The point is that you give up any notion of having optimal code the
> minute you start using something like numpy. And you do so happily
> for the ability to get stuff done faster and have nicer looking, more
> readable code in the end. When everything works, that's when you hit
> the "go fast button" if you even really need to.
That's what numexpr is for.
A = numexpr.evaluate("B + C - 2*D")
Evaluates this considerably faster than numpy and doesn't chew up all
that extra memory. Actually that's why it's faster -- it's much more
memory friendly. It's also a lot less flexible, although that's been
improving. Still you want numpy as a base, but numexpr is a viable
alternative for those critical sections where once you would resort to
x= or worse, three argument ufuncs.
More information about the Numpy-discussion