如果您希望您的函数参数仍然可用,您需要以创造性的方式使用布尔索引并将您的函数替换为:
from time import time
import numpy as np
ran = np.arange(-10, 10, 1)
s = 2
r = 3
st = s * r
def func(S, R, H):
ST = S * R
if ST <= - H:
result = - H
elif ST >= - H and ST < 0:
result = ST
else:
result = min(ST, H)
return result
# calculate with function
a = []
t1 = time()
for x in ran:
a.append(func(s, r, x))
t2 = time()
print("time with function:", t2 - t1)
a = np.array(a)
# calculate with numpy
y = np.copy(ran)
neg_y = np.copy(y) * -1
# creative boolean indexing
t1 = time()
y[st <= neg_y] = neg_y[st <= neg_y]
if st < 0:
y[st >= neg_y] = st
else:
alike = np.full(ran.shape, st)[st >= neg_y]
y[st > neg_y] = np.where(y[st > neg_y] > st, st, y[st > neg_y])
t2 = time()
print(a)
print(y)
print("time with numpy indexing:", t2 - t1)
会给你(时间省略):
# s=2, r=3
[10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -6 -6 -6] # function
[10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -6 -6 -6] # numpy
# s=-2, s=3
[10 9 8 7 6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 6 6 6] # function
[10 9 8 7 6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 6 6 6] # numpy
您可能需要对其进行更多调整。
用一个
ran = np.arange(-1000, 1000, 0.001)
我得到的时间(s = 3,r = 5):
time with function: 5.606577634811401
[1000. 999.999 999.998 ... 15. 15. 15. ]
[1000. 999.999 999.998 ... 15. 15. 15. ]
time with numpy indexing: 0.06600046157836914