[Numpy-discussion] custom accumlators

Matt Knox mattknox_ca at hotmail.com
Sat Jan 6 01:21:44 EST 2007


> Are you sure about this? I ran this case using timeit, and the first one 
> was 5 times or so *faster* than the second case. I just dug around and 
> frompyfunc is acutally implemented in C, although it has to call back 
> into python to execute the function being vectorized. Can you try using 
> timeit instead of profile and see what you get? For example:
> 
>     a = np.cumprod(1 + np.random.normal(size=10000)/10)
> 
>     if __name__ == '__main__':
>         from timeit import Timer
>         print Timer('expmave1(a, .5)', 'from scratch import np, 
> expmave1, a').timeit(10)
>         print Timer('expmave2(a, .5)', 'from scratch import np, 
> expmave2, a').timeit(10)
> 
> Anyway, I'm glad that all was helpful.
> 
> -tim
> 
> -tim
> 

wow, you're right. Good call. profile and timeit give conflicting results (and 
indeed, the timeit results are more accurate, I made a manual timer to test and 
compared). I'll have to be more careful using profile in the future.

So that's pretty awesome then, a 5 times speed up using frompyfunc and 
accumulate vs the brute force way of doing it. And I think the code looks a 
little nicer too which is always nice. Definitely a technique I will be using 
in the future.

- Matt




More information about the NumPy-Discussion mailing list