[Numpy-discussion] how do I speed up this?

Giorgio F. Gilestro giorgio at gilestro.tk
Tue Nov 27 14:41:57 EST 2007


Thanks Tim,
shape of the array is variable but follows the rule (x, y*1440) with 
both x an y being values greater than 1 (usually around 10 or 20).
So as result second dimension is always much bigger. (and the bug you 
foresaw is actually taken care of)
I figured out that somehow removing the unnecessary *1 multiplication 
after the boolean verification will increase speed by almost 100% (wow!).
I guess if I can get rid of the iteration too and use some internal 
function I'd probably get even faster.
Could .flat() help me somehow?

Timothy Hochberg wrote:
>
>
> On Nov 27, 2007 11:38 AM, Giorgio F. Gilestro <giorgio at gilestro.tk 
> <mailto:giorgio at gilestro.tk>> wrote:
>
>     Hello everyone,
>
>     ma and new_ma are bi-dimensional array with shape (a1, a2) on
>     which I am
>     performing the following iteration:
>
>     for fd in range(a1):
>         new_ma[fd] = [( ma[fd][i-5:i+5].sum() == 0 )*1 for i in range
>     (a2)] 
>
>
> This looks like it's probably buggy; when, for example,, a2 == 0, I 
> don't think ma[fd][-5:5] is going to return what you want.
>
>
>     Is there any faster more elegant way to do that with numpy?
>
>
> There are faster ways, I'm not sure that they are more elegant. It 
> looks like what you want is essentially a moving average, possible 
> with periodic boundary conditions. There are a few different tricks 
> that can make this fast; which is appropriate depends on what the 
> shape of ma is expected to be. If 'a1' if large, then you can 
> profitably remove the outer loop; something vaguely like:
>
> for i in range(a2):
>     new_ma[:, i] = ( ma[fd][i-5:i+5].sum() == 0)
>
> (This is still buggy as above, since I'm not sure what kind of 
> boundary conditions you need).
>
> If that's not applicable, another approach is to loop over the offset, 
> in this case -5 to 5, and remove the loop over a2. That method is more 
> complicated and I'm going to avoid describing it in detail unless 
> needed. One might even be able to do both, but that's probably overkill.
>  
> HTH
>
> -- 
> .  __
> .   |-\
> .
> .  tim.hochberg at ieee.org <mailto:tim.hochberg at ieee.org>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>   


-- 
giorgio at gilestro.tk
http://www.cafelamarck.it





More information about the NumPy-Discussion mailing list