相关疑难解决方法(0)

为什么numpy sum比+运算符慢10倍?

我注意到,非常紧张,np.sum比手写总和慢10倍.

带轴的np.sum:

p1 = np.random.rand(10000, 2)
def test(p1):
    return p1.sum(axis=1)
%timeit test(p1)
Run Code Online (Sandbox Code Playgroud)

每回路186μs±4.21μs(平均值±标准偏差,7次运行,每次1000次循环)

没有轴的np.sum:

p1 = np.random.rand(10000, 2)
def test(p1):
    return p1.sum()
%timeit test(p1)
Run Code Online (Sandbox Code Playgroud)

每回路17.9μs±236 ns(平均值±标准偏差,7次运行,每次10000次循环)

+:

p1 = np.random.rand(10000, 2)
def test(p1):
    return p1[:,0] + p1[:,1]
%timeit test(p1)
Run Code Online (Sandbox Code Playgroud)

每个环路15.8μs±328 ns(平均值±标准偏差,7次运行,每次100000次循环)

乘法:

p1 = np.random.rand(10000, 2)
def test(p1):
    return p1[:,0]*p1[:,1]
%timeit test(p1)
Run Code Online (Sandbox Code Playgroud)

每个环路15.7μs±701 ns(平均值±标准偏差,7次运行,每次10000次循环)

我没有看到任何理由.知道为什么吗?我的numpy版本是1.15.3.

编辑:10000000:

np.sum (with axis): 202 ms (5 x)
np.sum (without axis): 12 ms
+ : 46 ms (1 x)
* : 44.3 ms …
Run Code Online (Sandbox Code Playgroud)

python performance numpy

13
推荐指数
1
解决办法
753
查看次数

解释与Python中的动态调度惩罚

我看了Brandon Rhodes关于Cython的演讲 - "EXE的日子在我们身上".

布兰登在09:30提到,对于一段特定的短代码,跳过解释给出了40%的加速,而跳过分配和发送则给出了574%的加速(10:10).

我的问题是 - 如何测量特定的代码?是否需要手动提取底层c命令,然后以某种方式使运行时运行它们?

这是一个非常有趣的观察,但我如何重新创建实验呢?

python dynamic-programming performance-testing cython dispatch

4
推荐指数
1
解决办法
341
查看次数