[Numpy-discussion] A surprising result from benchmarking

Dan Becker dbecker@alum.dartmouth....
Sun Mar 11 00:02:13 CST 2007


Hi Everyone,

I'm new to numpy, and I'm finding it hard to predict what is fast in
python/numpy and what is slow.  The following seems puzzling: I am doing the
same thing an ugly way and a cleaner way.  But the ugly map/lambda/filter
expression is 15x faster than using numpy's internals.

Can anyone explain why?  For now, this makes me nervous about incorporating
basic numpy functionality into real programs. 

---Code starts here---
import scipy
import time
import psyco
from numpy import matrix

print("New run")
myMat=scipy.randn(500,500)

t1=time.time()
highEnough=myMat>0.6
greaterPerLine=[sum(x) for x in highEnough]
elapsed1=time.time()-t1
print("method 1 took %f seconds"%elapsed1)

t2=time.time()
greaterPerLine2=map(lambda(x):len(filter(lambda(y):y>0.6,x)),myMat)
elapsed2=time.time()-t2
print("method 2 took %f seconds"%elapsed2)

---Output starts here---
New run
method 1 took 3.566760 seconds
method 2 took 0.232356 seconds

---

Thanks so much!
Dan



More information about the Numpy-discussion mailing list