我不確定 out 引數是否值得我的專案麻煩,所以我正在做一系列測驗。
但到目前為止,在我測驗過的每一種情況下,如果與更簡單的實作不同,out 引數似乎會使性能稍微變慢,我不知道為什么。
這是一個示例:
test1并且test11是等效的,但后者使用 out 引數來避免每次都分配新陣列。
test2和_test22
import numpy as np
from timeit import timeit
N = 1000
weights = np.arange(N * N).astype('f4').reshape((N, N))
inputs = np.arange(N).astype('f4')
cache_out1 = np.empty((N, N), dtype='f4')
cache_out2 = np.empty(N, dtype='f4')
def test1():
return (weights * inputs).sum(axis=1)
def test11():
np.multiply(weights, inputs, out=cache_out1)
np.sum(cache_out1, axis=1, out=cache_out2)
return cache_out2
def test2():
return (weights * inputs[:, np.newaxis]).sum(axis=0)
def test22():
np.multiply(weights, inputs[:, np.newaxis], out=cache_out1)
np.sum(cache_out1, axis=0, out=cache_out2)
return cache_out2
print('test1:', timeit(test1, number=1000))
print('test11:', timeit(test11, number=1000))
print('test2:', timeit(test2, number=1000))
print('test22:', timeit(test22, number=1000))
輸出:
test1: 1.1015455439919606
test11: 1.0834621820104076
test2: 1.1083468289871234
test22: 1.1045935050060507
uj5u.com熱心網友回復:
當您的陣列更大并且分配時間更長時,它將產生更大的影響。使用該out引數將允許您通過重新使用該記憶體來分攤分配時間。舉個例子:
import numpy as np
from timeit import timeit
N = 4096
weights = np.arange(N * N).astype('f4').reshape((N, N))
inputs = np.arange(N).astype('f4')
cache_out1 = np.empty((N, N), dtype='f4')
cache_out2 = np.empty(N, dtype='f4')
def test1():
return (weights * inputs).sum(axis=1)
def test11():
np.multiply(weights, inputs, out=cache_out1)
np.sum(cache_out1, axis=1, out=cache_out2)
return cache_out2
def test2():
return (weights * inputs[:, np.newaxis]).sum(axis=0)
def test22():
np.multiply(weights, inputs[:, np.newaxis], out=cache_out1)
np.sum(cache_out1, axis=0, out=cache_out2)
return cache_out2
n = 100
print('test1:', timeit(test1, number=n))
print('test11:', timeit(test11, number=n))
print('test2:', timeit(test2, number=n))
print('test22:', timeit(test22, number=n))
test1: 2.5047981239913497
test11: 1.7144565229973523
test2: 2.4683585959865013
test22: 1.6845238849928137
uj5u.com熱心網友回復:
如果您真的想壓縮最后的納秒,請檢查 Numba 或 Cython。
特別是如果您可以并行操作,您可以獲得非常重要的加速。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/479479.html
