
Hi all, I'm porting some code from old scipy to new scipy, and I've run into a rather large performance problem. The heart of the code is integrating a system of nonlinear differential equations using odeint. The function that dominates the time to run calculates the right hand side, given a current state x. (len(x) ~ 50.) Abstracted, the function looks like: def rhs(x) output = scipy.zeros(10, scipy.Float) a = x[0] b = x[1] ... output[0] = a/b + c*sqrt(d)... output[1] = b-a + 2*b... ... return output (I copy the elements of the current state to local variables to avoid the cost of repeatedly calling x.__getitem__, and to make the resulting equations easier to read.) When using numpy, a and b are now array scalars and the arithmetic is much slower, resulting in about a factor of 10 increase in runtimes from those using Numeric. I've tried doing: a = x[0].item(), which allows the arimetic be done on pure scalars. This is a little faster, but still results in a factor of 3 increase in runtime from old scipy. I imagine the slowdown comes from having to call __getitem__() followed by item() So questions: 1) I haven't followed the details of the array scalar discussions. Is it anticipated that array scalar arithmetic will eventually be as fast as arithmetic in native python types? 2) If not, is it possible to get a "pure" scalar directly from an array in one function call? Thanks for any help, Ryan -- Ryan Gutenkunst | Cornell LASSP | "It is not the mountain | we conquer but ourselves." Clark 535 / (607)227-7914 | -- Sir Edmund Hillary AIM: JepettoRNG | http://www.physics.cornell.edu/~rgutenkunst/

Ryan Gutenkunst wrote:
So questions: 1) I haven't followed the details of the array scalar discussions. Is it anticipated that array scalar arithmetic will eventually be as fast as arithmetic in native python types?
More or less, if I'm not mistaken. This ticket is aimed at that: http://projects.scipy.org/scipy/numpy/ticket/55
2) If not, is it possible to get a "pure" scalar directly from an array in one function call?
float(x[0]) seems to be faster on my PowerBook. -- Robert Kern robert.kern@gmail.com "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco

On Apr 20, 2006, at 5:18 PM, Robert Kern wrote:
Ryan Gutenkunst wrote:
So questions: 1) I haven't followed the details of the array scalar discussions. Is it anticipated that array scalar arithmetic will eventually be as fast as arithmetic in native python types?
More or less, if I'm not mistaken. This ticket is aimed at that:
Good to hear.
2) If not, is it possible to get a "pure" scalar directly from an array in one function call?
float(x[0]) seems to be faster on my PowerBook.
It's faster for me, too, but float(x[0]) is still much slower than using Numeric where x[0] suffices. I guess I'll just have to warn my users away from the new scipy until numpy 0.9.8 comes out and scalar math is sped up. Cheers, Ryan -- Ryan Gutenkunst | Cornell Dept. of Physics | "It is not the mountain | we conquer but ourselves." Clark 535 / (607)255-6068 | -- Sir Edmund Hillary AIM: JepettoRNG | http://www.physics.cornell.edu/~rgutenkunst/

Ryan Gutenkunst wrote:
On Apr 20, 2006, at 5:18 PM, Robert Kern wrote:
Ryan Gutenkunst wrote:
2) If not, is it possible to get a "pure" scalar directly from an array in one function call?
float(x[0]) seems to be faster on my PowerBook.
It's faster for me, too, but float(x[0]) is still much slower than using Numeric where x[0] suffices. I guess I'll just have to warn my users away from the new scipy until numpy 0.9.8 comes out and scalar math is sped up.
For that matter, a plain "x[0]" seems to be about 3x faster with Numeric than numpy. -- Robert Kern robert.kern@gmail.com "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco

Robert Kern wrote:
Ryan Gutenkunst wrote:
On Apr 20, 2006, at 5:18 PM, Robert Kern wrote:
Ryan Gutenkunst wrote:
2) If not, is it possible to get a "pure" scalar directly from an array in one function call?
float(x[0]) seems to be faster on my PowerBook.
It's faster for me, too, but float(x[0]) is still much slower than using Numeric where x[0] suffices. I guess I'll just have to warn my users away from the new scipy until numpy 0.9.8 comes out and scalar math is sped up.
For that matter, a plain "x[0]" seems to be about 3x faster with Numeric than numpy.
We are already special-casing the integer select code but could special-case the getitem code so that if nd==1 a faster construction is used. I think right now a 0-dim array is being created only to get destroyed later on return. Please add a ticket as this extremely common operation should be made as fast as possible. This is a little tricky because array_big_item is called in a few places and is expected to return an array. If it returns a scalar in those places segfaults can occur. Either checks need to be made in each of those cases or the special-casing needs to be in array_big_item_nice. I'm not sure which I prefer.... -Travis
participants (3)
-
Robert Kern
-
Ryan Gutenkunst
-
Travis Oliphant