为何Numba切片操作比Numpy切片操作快这么多?
我发现Numba切片操作比Numpy切片操作快很多,以下是测试过程:
def test(x): k = x[1:2] l = x[0:3] m = x[0:1] @njit def test2(x): k = x[1:2] l = x[0:3] m = x[0:1] x = np.arange(5) test2(x) %timeit test(x) %timeit test2(x)
计时结果:
776 ns ± 1.83 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
280 ns ± 2.53 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
随着切片次数增加,两者的性能差距进一步扩大:
def test(x): k = x[1:2] l = x[0:3] m = x[0:1] n = x[1:3] o = x[2:3] @njit def test2(x): k = x[1:2] l = x[0:3] m = x[0:1] n = x[1:3] o = x[2:3] test2(x) %timeit test(x) %timeit test2(x)
计时结果:
1.18 µs ± 1.82 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
279 ns ± 0.562 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Numpy函数的耗时随切片次数线性增长,而Numba函数的耗时几乎不受切片次数影响(原本我以为两者都会是这样的表现)
编辑补充测试
在看到chrslg的回答后,我给两个函数都添加了返回语句:
return k,l,m,n,o
此时的计时结果为:
1.23 µs ± 2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
1.61 µs ± 9.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
此时Numba函数反而更慢了,这似乎说明之前的代码是无效代码。不过在看到用户jared的评论后,我尝试对切片结果执行乘积操作:
def test5(x): k = x[1:2] l = x[0:3] m = x[0:1] n = x[1:4] o = x[2:3] return (k*l*m*n*o) @njit def test6(x): k = x[1:2] l = x[0:3] m = x[0:1] n = x[1:4] o = x[2:3] return (k*l*m*n*o) test6(x) %timeit test5(x) %timeit test6(x)
计时结果:
5.79 µs ± 202 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
787 ns ± 1.52 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
现在Numba函数又比仅返回切片的情况更快了,性能差距再次拉大,这让我更加困惑了。
内容的提问来源于stack exchange,提问作者Klaus3

