2011年07月16日 星期六 11:48
<code>
point1 = [ [-24.467 15.998 -5.568]
[-23.781 15.369 -6.762]
[-24.498 14.687 -7.683]]
point2 = [ [-24.819 16.174 -5.351]
[-24.031 15.62 -6.52 ]
[-24.632 14.834 -7.44 ]]
def distance2(p1, p2):
d2 = .0
for i in range(3):
d2 += (p1[i] - p2[i]) ** 2
return d2
def rmsd(point1, point2):
n = len(point1)
for i in range(n):
R2 += distance2(point1[i], point2[i])
return math.sqrt(R2 / n)
if __name__ == "__main__":
RMSD = rmsd(point1, point2)
print(RMSD)
</code>
我用numpy来实现的代码怎么都比原生的python代码慢。请问下,怎么实现才能速度变快?
用ufunc能行吗?
2011年07月16日 星期六 18:51
numpy的调用开销比Python大,因此对于很小的数组,Python可能会比numpy快。如果数据量大的话,那么numpy会比Python快很多,例如我将你的数据量放大100倍,得到的结果如下:
python: 1.63338135183
numpy: 0.0589712179874
下面是程序:
import math
import numpy as np
point1 = [ [-24.467, 15.998, -5.568],
[-23.781, 15.369, -6.762],
[-24.498, 14.687, -7.683]]
point2 = [ [-24.819, 16.174, -5.351],
[-24.031, 15.62, -6.52 ],
[-24.632, 14.834, -7.44 ]]
point1 = point1 * 100
point2 = point2 * 100
def distance2(p1, p2):
d2 = .0
for i in range(3):
d2 += (p1[i] - p2[i]) ** 2
return d2
def rmsd(point1, point2):
n = len(point1)
R2 = 0
for i in range(n):
R2 += distance2(point1[i], point2[i])
return math.sqrt(R2 / n)
def rmsd_numpy(p1, p2):
return math.sqrt(np.sum((p1-p2)**2) / len(point1))
if __name__ == "__main__":
import time
start = time.clock()
for i in xrange(1000):
rmsd(point1, point2)
print "python:", time.clock() - start
start = time.clock()
p1 = np.array(point1)
p2 = np.array(point2)
for i in xrange(1000):
rmsd_numpy(p1, p2)
print "numpy:", time.clock() - start
2011年07月21日 星期四 14:43
非常感谢。之前我distance直接用
def distance2(m, n):
sq = np.linalg.norm(m - n) ** 2
return sq
或:
def distance2(m, n):
sq = ((m - n) ** 2).sum()
return sq
Zeuux © 2024
京ICP备05028076号