From: Ryan G. <rn...@co...> - 2006-09-13 18:32:35
|
Hi all, I'm migrating an application from Numeric to numpy, and I've run into a significant application slowdown related to arithmetic on array-scalars. The inner loop of the application is integrating a nonlinear set of differential equations using odeint, with the rhs a dynamically-generated (only once) python function. In that function I copy the entries of the current x array to a bunch of local variables, do a bunch of arithmetic, and assign the results to a dx_dt array. The arithmetic is approximately 3x slower using numpy than Numeric, because numpy returns array-scalars while Numeric returns normal scalars. (Simple example below.) I can wrap all my arrays accesses with float() casts, but that introduces a noticable overhead (~50% for problems of interest). I'm guessing speeding up the scalar-array math would be difficult, if not impossible. (Maybe I'm wrong?) I notice that numpy_array.item() will give me the first element as a normal scalar. Would it be possible for numpy_array.item(N) to return the Nth element of the array as a normal scalar? Thanks a bunch, Ryan The effect can be isolated as (running in python 2.4 on a 32-bit Athlon): In [1]: import Numeric, numpy In [2]: a_old, a_new = Numeric.array([1.0, 2.0]), numpy.array([1.0, 2.0]) In [3]: b_old, b_new = a_old[0], a_new[0] In [4]: %time for ii in xrange(1000000):c = b_old + 1.0 CPU times: user 0.40 s, sys: 0.00 s, total: 0.40 s Wall time: 0.40 In [5]: %time for ii in xrange(1000000):c = b_new + 1.0 CPU times: user 1.20 s, sys: 0.00 s, total: 1.20 s Wall time: 1.22 In [6]: Numeric.__version__, numpy.__version__ Out[6]: ('24.2', '1.0b5') -- 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/ |
From: Travis O. <oli...@ie...> - 2006-09-13 19:06:44
|
Ryan Gutenkunst wrote: > Hi all, > > I'm migrating an application from Numeric to numpy, and I've run into a > significant application slowdown related to arithmetic on array-scalars. > > Yeah, that is a common thing. There are two factors: 1) array indexing and 2) array scalar math. I don't think array scalar math has to be slower in principle then Python code. I think it's ability to handle interaction with multiple scalars that is operating more slowly right now. It could be sped up. The array indexing code is slower (in fact Numeric's indexing code is slower then just using lists also). > I'm guessing speeding up the scalar-array math would be difficult, if > not impossible. (Maybe I'm wrong?) > I think scalar-array math could be sped up quite a bit. I haven't done much in that area at all. Right now a lot of setup code is handled generically instead of type-specifically like it could be. > I notice that numpy_array.item() will give me the first element as a > normal scalar. Would it be possible for numpy_array.item(N) to return > the Nth element of the array as a normal scalar? > Now this is an interesting idea. It would allow you to by-pass the slow-indexing as well as the array scalar computation should you desire it. I like it and am going to add it unless there are convincing objections. -Travis |
From: Ryan G. <rn...@co...> - 2006-09-14 22:15:18
|
Hi Travis, Travis Oliphant wrote: > Ryan Gutenkunst wrote: >> I notice that numpy_array.item() will give me the first element as a >> normal scalar. Would it be possible for numpy_array.item(N) to return >> the Nth element of the array as a normal scalar? >> > Now this is an interesting idea. It would allow you to by-pass the > slow-indexing as well as the array scalar computation should you desire > it. I like it and am going to add it unless there are convincing > objections. > > -Travis Thanks for the quick response, but either there's a bug, or I'm using things wrong. It appears to work to work in the N-D case, but not 1-D. I looked at the change you made, but my grasp of the C-API is too weak to isolate the problem. >>> import numpy >>> numpy.__version__ '1.0rc1.dev3154' >>> a = numpy.array([[1.0, 2.0], [3.0, 4.0]]) >>> a.item(1,1) 4.0 >>> a = numpy.array([1.0, 2.0]) >>> a.item(0) 1.0 >>> a.item(1) 1.7765824089018436e-307 >>> a.item((1,)) 1.7765824089018436e-307 Thanks for your 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/ |
From: Travis O. <oli...@ee...> - 2006-09-14 23:41:09
|
Ryan Gutenkunst wrote: >Hi Travis, > >Travis Oliphant wrote: > > >>Ryan Gutenkunst wrote: >> >> >>>I notice that numpy_array.item() will give me the first element as a >>>normal scalar. Would it be possible for numpy_array.item(N) to return >>>the Nth element of the array as a normal scalar? >>> >>> >>> >>Now this is an interesting idea. It would allow you to by-pass the >>slow-indexing as well as the array scalar computation should you desire >>it. I like it and am going to add it unless there are convincing >>objections. >> >>-Travis >> >> > >Thanks for the quick response, but either there's a bug, or I'm using >things wrong. It appears to work to work in the N-D case, but not 1-D. I >looked at the change you made, but my grasp of the C-API is too weak to >isolate the problem. > > It's a silly bug. Please check out the latest SVN. -Travis |
From: Ryan G. <rn...@co...> - 2006-09-15 01:00:04
|
Travis, Thanks for the quick response. My application is back up to its old speed. Thanks also for spearheading the numpy/scipy projects. It's certainly made my work much, much more productive. Cheers, Ryan On Sep 14, 2006, at 7:40 PM, Travis Oliphant wrote: > Ryan Gutenkunst wrote: >> Thanks for the quick response, but either there's a bug, or I'm using >> things wrong. It appears to work to work in the N-D case, but not >> 1-D. I >> looked at the change you made, but my grasp of the C-API is too weak >> to >> isolate the problem. >> >> > It's a silly bug. Please check out the latest SVN. > > -Travis -- 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/ |