On 7/29/06, Charles R Harris <[EMAIL PROTECTED]> wrote:
Hmmm,

I rewrote the subroutine a bit.


def numpy_nmean(list,n):
    a = numpy.empty(len(list),dtype=float)
    b = numpy.cumsum(list)
    for i in range(0,len(list)):
        if i < n :
            a[i] = b[i]/(i+1)
        else :
            a[i] = (b[i] - b[i-n])/(i+1)
    return a

and got

regular python took: 0.750000 sec.
numpy took: 0.380000 sec.

I got rid of the for loop entirely. Usually this is the thing to do, at least this will always give speedups in Matlab and also in my limited experience with Numpy/Numeric:

def numpy_nmean2(list,n):
    a = numpy.empty(len(list),dtype=float)
    b = numpy.cumsum(list)
    c = concatenate((b[n:],b[:n]))
    a[:n] = b[:n]/(i+1)
    a[n:] = (b[n:] - c[n:])/(i+1)
    return a
 

I got no noticeable speedup from doing this which I thought was pretty amazing. I even profiled all the functions, the original, the one written by Charles, and mine, using hotspot just to make sure nothing funny was going on. I guess plain old Python can be better than you'd expect in certain situtations.

--
David Grant
http://www.davidgrant.ca
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion

Reply via email to