我将数组Amp乘以另一个B ** w,其中W是另一个数组,然后将每个w的结果数组相加。
Amp和B的大小(4867206,1),W的大小(40x10 ^ 3,1)。
如果W是大小(1000,1),目前这需要2分49秒。当使用尺寸为40x10 ^ 3的完整W时,如何提高此速度?
Hw2=[np.einsum('i,i->', Amp, (np.array(B)**w)) for w in W]
答案 0 :(得分:1)
有两件事可以让你获得健康的加速:
1)您不希望列表中包含array
工厂。它实际上很慢。
2)计算log(B)
,然后使用exp
代替**
。这节省了很多。
>>> Amp = np.random.random(4867206)
>>> B = np.random.random(4867206)
>>> W = 10 * np.random.random(40000) + 1
>>>
>>> from time import perf_counter
>>>
>>> t = perf_counter(); logB = np.log(B); s = perf_counter()
>>> s-t
0.1715415450744331
>>>
>>> t = perf_counter(); [np.einsum('i,i->', Amp, B**w) for w in W[:40]]; s = perf_counter()
[232552.87174648093, 307130.7390907966, 411262.86511309125, 361323.4099230686, 254219.3700454278, 291692.2455839877, 324589.6747811661, 762459.3664474463, 224831.38520298406, 501641.86340860004, 466934.72400738456, 441544.52557156974, 995259.4253344169, 207811.00874071234, 408355.53573396447, 269901.94895861426, 304678.5850806002, 208719.98547583033, 318300.7763362345, 271688.90632957884, 388056.3735974982, 362437.1587603325, 456415.8506358219, 567634.1566253774, 418715.1493866043, 698332.545166694, 711861.6705545874, 391412.016841215, 569291.0132128834, 331811.20195587486, 898976.2873925611, 230896.99034275368, 225609.32356150646, 220438.15228011008, 526091.9360881918, 388536.063436256, 297158.4095318841, 382482.6531720307, 234679.1485575674, 263925.33778147714]
>>> s-t
15.207583270967007
>>>
>>> t = perf_counter(); [np.einsum('i,i->', Amp, np.exp(w * logB)) for w in W[:40]]; s = perf_counter()
[232552.87174648093, 307130.7390907966, 411262.8651130912, 361323.4099230686, 254219.3700454278, 291692.2455839877, 324589.6747811661, 762459.3664474462, 224831.38520298406, 501641.86340860004, 466934.72400738456, 441544.52557156974, 995259.4253344169, 207811.00874071234, 408355.5357339644, 269901.9489586143, 304678.5850806002, 208719.98547583033, 318300.7763362345, 271688.90632957884, 388056.3735974982, 362437.1587603325, 456415.8506358219, 567634.1566253774, 418715.1493866043, 698332.545166694, 711861.6705545874, 391412.016841215, 569291.0132128834, 331811.20195587486, 898976.2873925611, 230896.99034275368, 225609.32356150646, 220438.15228011008, 526091.9360881918, 388536.063436256, 297158.4095318842, 382482.6531720308, 234679.1485575674, 263925.33778147714]
>>> s-t
5.111462005996145