import numpy as np
from numpy.random import random
@profile
def point_func(point, points, funct):
return np.sum(funct(np.sqrt(((point - points)**2)).sum(1)))
@profile
def point_afunc(ipoints, epoints, funct):
res = np.zeros(len(ipoints))
for idx, point in enumerate(ipoints):
res[idx] = point_func(point, epoints, funct)
return res
@profile
def main():
points = random((5000,3))
rpoint = random((1,3))
pres = point_func(rpoint, points, lambda r : r**3)
ares = point_afunc(points, points, lambda r : r**3)
if __name__=="__main__":
main()
我使用对其进行了分析,得到了以下结果:
Timer unit: 1e-06 s
Total time: 2.25667 s File: point-array-vectorization.py Function: point_func at line 4
Line # Hits Time Per Hit % Time Line Contents
==============================================================
4 @profile
5 def point_func(point, points, funct):
6 5001 2256667.0 451.2 100.0 return np.sum(funct(np.sqrt(((point - points)**2)).sum(1)))
Total time: 2.27844 s File: point-array-vectorization.py Function: point_afunc at line 8
Line # Hits Time Per Hit % Time Line Contents
==============================================================
8 @profile
9 def point_afunc(ipoints, epoints, funct):
10 1 5.0 5.0 0.0 res = np.zeros(len(ipoints))
11 5001 4650.0 0.9 0.2 for idx, point in enumerate(ipoints):
12 5000 2273789.0 454.8 99.8 res[idx] = point_func(point, epoints, funct)
13 1 0.0 0.0 0.0 return res
Total time: 2.28239 s File: point-array-vectorization.py Function: main at line 15
Line # Hits Time Per Hit % Time Line Contents
==============================================================
15 @profile
16 def main():
17 1 145.0 145.0 0.0 points = random((5000,3))
18 1 2.0 2.0 0.0 rpoint = random((1,3))
19
20 1 507.0 507.0 0.0 pres = point_func(rpoint, points, lambda r : r**3)
21
22 1 2281731.0 2281731.0 100.0 ares = point_afunc(points, points, lambda r : r**3)
所以这部分占用了大部分时间:
11 5001 4650.0 0.9 0.2 for idx, point in enumerate(ipoints):
12 5000 2273789.0 454.8 99.8 res[idx] = point_func(point, epoints, funct)
我想确定是否因为在 for
循环中调用 funct
导致时间损失。为了做到这一点,我想要使用 numpy.vectorize
将 point_afunc
向量化。我已经尝试过了,但它似乎将点向量化掉了:循环最终遍历单个点组件。
@profile
def point_afunc(ipoints, epoints, funct):
res = np.zeros(len(ipoints))
for idx, point in enumerate(ipoints):
res[idx] = point_func(point, epoints, funct)
return res
point_afunc = np.vectorize(point_afunc)
导致错误:
File "point-array-vectorization.py", line 24, in main
ares = point_afunc(points, points, lambda r : r**3)
File "/usr/lib/python3.6/site-packages/numpy/lib/function_base.py", line 2755, in __call__
return self._vectorize_call(func=func, args=vargs)
File "/usr/lib/python3.6/site-packages/numpy/lib/function_base.py", line 2825, in _vectorize_call
ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
File "/usr/lib/python3.6/site-packages/numpy/lib/function_base.py", line 2785, in _get_ufunc_and_otypes
outputs = func(*inputs)
File "/usr/lib/python3.6/site-packages/line_profiler.py", line 115, in wrapper
result = func(*args, **kwds)
File "point-array-vectorization.py", line 10, in point_afunc
res = np.zeros(len(ipoints))
TypeError: object of type 'numpy.float64' has no len()
不知何故,它没有将ipoints
中的每个点向量化,而是在点的组成部分之间进行了向量化?
编辑:尝试了@John Zwinck的建议并使用了numba。我发现使用@jit
的执行时间比不使用更长。如果我从所有函数中删除@profile
装饰器,并将其替换为point_func
和point_afunc
的@jit
,则执行时间如下:
time ./point_array_vectorization.py
real 0m3.686s
user 0m3.584s
sys 0m0.077s
point-array-vectorization> time ./point_array_vectorization.py
real 0m3.683s
user 0m3.596s
sys 0m0.063s
point-array-vectorization> time ./point_array_vectorization.py
real 0m3.751s
user 0m3.658s
sys 0m0.070s
并且移除了所有的@jit
装饰器:
point-array-vectorization> time ./point_array_vectorization.py
real 0m2.925s
user 0m2.874s
sys 0m0.030s
point-array-vectorization> time ./point_array_vectorization.py
real 0m2.950s
user 0m2.902s
sys 0m0.029s
point-array-vectorization> time ./point_array_vectorization.py
real 0m2.951s
user 0m2.886s
sys 0m0.042s
我需要更多地帮助numba
编译器吗?
编辑: 可以使用numpy
在不使用for循环的情况下编写 point_afunc
吗?
编辑: 通过Peter的广播版本将循环版本进行比较,循环版本更快:
Timer unit: 1e-06 s
Total time: 2.13361 s
File: point_array_vectorization.py
Function: point_func at line 7
Line # Hits Time Per Hit % Time Line Contents
==============================================================
7 @profile
8 def point_func(point, points, funct):
9 5001 2133615.0 426.6 100.0 return np.sum(funct(np.sqrt(((point - points)**2)).sum(1)))
Total time: 2.1528 s
File: point_array_vectorization.py
Function: point_afunc at line 11
Line # Hits Time Per Hit % Time Line Contents
==============================================================
11 @profile
12 def point_afunc(ipoints, epoints, funct):
13 1 5.0 5.0 0.0 res = np.zeros(len(ipoints))
14 5001 4176.0 0.8 0.2 for idx, point in enumerate(ipoints):
15 5000 2148617.0 429.7 99.8 res[idx] = point_func(point, epoints, funct)
16 1 0.0 0.0 0.0 return res
Total time: 2.75093 s
File: point_array_vectorization.py
Function: new_point_afunc at line 18
Line # Hits Time Per Hit % Time Line Contents
==============================================================
18 @profile
19 def new_point_afunc(ipoints, epoints, funct):
20 1 2750926.0 2750926.0 100.0 return np.sum(funct(np.sqrt((ipoints[:, None, :] - epoints[None, :, :])**2).sum(axis=-1)), axis=1)
Total time: 4.90756 s
File: point_array_vectorization.py
Function: main at line 22
Line # Hits Time Per Hit % Time Line Contents
==============================================================
22 @profile
23 def main():
24 1 170.0 170.0 0.0 points = random((5000,3))
25 1 4.0 4.0 0.0 rpoint = random((1,3))
26 1 546.0 546.0 0.0 pres = point_func(rpoint, points, lambda r : r**3)
27 1 2155829.0 2155829.0 43.9 ares = point_afunc(points, points, lambda r : r**3)
28 1 2750945.0 2750945.0 56.1 vares = new_point_afunc(points, points, lambda r : r**3)
29 1 71.0 71.0 0.0 assert(np.max(np.abs(ares-vares)) < 1e-15)