此程序在我的程序中运行超过200万次。任何人都可以建议优化这个? x和y是元组。
def distance(x,y):
return sqrt((x[0]-y[0])*(x[0]-y[0])+(x[1]-y[1])*(x[1]-y[1])+(x[2]-y[2])*(x[2]-y[2]))
我的尝试:我尝试使用math.sqrt,pow和x **。但是没有太多的性能提升。
答案 0 :(得分:3)
你可以通过不访问相同的x [i]元素并在本地绑定它来削减一些周期。
def distance(x,y):
x0, x1, x2 = x
y0, y1, y2 = y
return sqrt((x0-y0)*(x0-y0)+(x1-y1)*(x1-y1)+(x2-y2)*(x2-y2))
例如,比较:
>>> import timeit
>>> timer = timeit.Timer(setup='''
... from math import sqrt
... def distance(x,y):
... return sqrt((x[0]-y[0])*(x[0]-y[0])+(x[1]-y[1])*(x[1]-y[1])+(x[2]-y[2])*(x[2]-y[2]))
... ''', stmt='distance((0,0,0), (1,2,3))')
>>> timer.timeit(1000000)
1.2761809825897217
使用:
>>> import timeit
>>> timer = timeit.Timer(setup='''
... from math import sqrt
... def distance(x,y):
... x0, x1, x2 = x
... y0, y1, y2 = y
... return sqrt((x0-y0)*(x0-y0)+(x1-y1)*(x1-y1)+(x2-y2)*(x2-y2))
... ''', stmt='distance((0,0,0), (1,2,3))')
>>> timer.timeit(1000000)
0.8375771045684814
答案 1 :(得分:1)
原件:
>>> timeit.timeit('distance2((0,1,2),(3,4,5))', '''
... from math import sqrt
... def distance2(x,y):
... return sqrt((x[0]-y[0])*(x[0]-y[0])+(x[1]-y[1])*(x[1]-y[1])+(x[2]-y[2])*(x[2]-y[2]))
... ''')
1.1989610195159912
消除常见的Subexpression:
>>> timeit.timeit('distance((0,1,2),(3,4,5))', '''
... def distance(x,y):
... d1 = x[0] - y[0]
... d2 = x[1] - y[1]
... d3 = x[2] - y[2]
... return (d1 * d1 + d2 * d2 + d3 * d3) ** .5''')
0.93855404853820801
优化解包:
>>> timeit.timeit('distance((0,1,2),(3,4,5))', '''
... def distance(x,y):
... x1, x2, x3 = x
... y1, y2, y3 = y
... d1 = x1 - y1
... d2 = x2 - y2
... d3 = x3 - y3
... return (d1 * d1 + d2 * d2 + d3 * d3) ** .5''')
0.90851116180419922
图书馆职能:
>>> timeit.timeit('distance((0,1,2),(3,4,5))', '''
... import math
... def distance(x,y):
... x1, x2, x3 = x
... y1, y2, y3 = y
... d1 = x1 - y1
... d2 = x2 - y2
... d3 = x3 - y3
... return math.sqrt(d1 * d1 + d2 * d2 + d3 * d3)
... ''')
0.78318595886230469
虚线:
>>> timeit.timeit('distance((0,1,2),(3,4,5))', '''
... from math import sqrt
... def distance(x,y):
... x1, x2, x3 = x
... y1, y2, y3 = y
... d1 = x1 - y1
... d2 = x2 - y2
... d3 = x3 - y3
... return sqrt(d1 * d1 + d2 * d2 + d3 * d3)
... ''')
0.75629591941833496
答案 2 :(得分:1)
from scipy.spatial.distance import euclidean
import numpy as np
# x and y are 1 x 3 vectors
x = np.random.rand(1,3)
y = np.random.rand(1,3)
euclidean(x,y)
编辑:实际上,通过timeit对OP的pure-python distance()函数运行,实际上这在python浮点数上变慢了。我认为scipy版本浪费了一些时间将花车投射到numpy dtypes。至少可以说我很惊讶。