[Numpy-discussion] Bypassing a[2].item()?

Ryan Gutenkunst rng7 at cornell.edu
Thu Apr 20 13:49:13 CDT 2006


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/




More information about the Numpy-discussion mailing list