From: Charles R H. <cha...@gm...> - 2006-07-29 17:20:52
|
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. The precision of the new routine is admittedly suspect. Convolve can also be used: def numpy_nmean_conv(list,n): b = numpy.ones(n,dtype=float) a = numpy.convolve(list,b,mode="full") for i in range(0,len(list)): if i < n : a[i] /= i + 1 else : a[i] /= n return a[:len(list)] Which gives numpy convolve took: 0.260000 sec. You might actually want mode="valid" which would only return averages over runs of length n. One oddity here is that I think convolution should be slower than cumsum and that raises my suspicions about the latter, it seems much too slow. Chuck On 7/29/06, Phil Ruggera <pru...@gm...> wrote: > > I rewrote some python code using numpy to do a performance comparison. > The results were the opposite of what I wanted. Numpy was slower > than Python without numpy. Is there something wrong with my approach? > > # mean of n values within an array > import numpy, time > def nmean(list,n): > a = [] > for i in range(1,len(list)+1): > start = i-n > divisor = n > if start < 0: > start = 0 > divisor = i > a.append(sum(list[start:i])/divisor) > return a > > t = [1.0*i for i in range(1400)] > start = time.clock() > for x in range(100): > nmean(t,50) > print "regular python took: %f sec."%(time.clock() - start) > > def numpy_nmean(list,n): > a = numpy.empty(len(list),dtype=float) > for i in range(1,len(list)+1): > start = i-n > if start < 0: > start = 0 > a[i-1] = list[start:i].mean(0) > return a > > t = numpy.arange(0,1400,dtype=float) > start = time.clock() > for x in range(100): > numpy_nmean(t,50) > print "numpy took: %f sec."%(time.clock() - start) > > > Results: > regular python took: 1.215274 sec. > numpy took: 2.499299 sec. > > ------------------------------------------------------------------------- > 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 > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion > |