【发布时间】:2020-07-21 16:03:18
【问题描述】:
我想要更快地实现如下所示的功能。理想情况下,当number_points 变量设置为 400-500 时,代码应该可以工作。有什么方法可以改进函数定义以提高速度(请参阅示例运行)?
这是我的代码:
import numpy as np
import time
def initialize_plane_points(Domain = 100,number_points=200,Plane_Offset=0.0):
'''Domain has implied coordinates of mm and the number of
points represents the number of samples within that space. '''
X = np.linspace(-Domain,Domain,number_points)
#print(X)
Y = np.linspace(-Domain,Domain,number_points)
#print(Y)
ZZ = np.array([])
XX,YY = np.meshgrid(X,Y)
for x in XX:
for y in YY:
ZZ = np.append(ZZ,[Plane_Offset])
ZZ = np.reshape(ZZ, (len(XX),len(YY)))
Shape = np.array([])
for i in range(len(XX)):
for j in range(len(YY)):
Shape = np.append(Shape,[XX[i,j],YY[i,j],ZZ[i,j]])
a = int(len(Shape) / 3)
SHAPE = np.reshape(Shape,(a,3))
return SHAPE
T0 = time.perf_counter()
Points = initialize_plane_points(number_points=100)
T1 = time.perf_counter()
print("100 initialize time: ",T1-T0)
【问题讨论】:
标签: python arrays vectorization numpy-ndarray numpy-einsum