2017-07-18 113 views
2

所以我需要改善我一直在努力的脚本的执行时间。我开始与numba JIT装饰工作,试图并行计算然而它抛出我numba @jit慢了一点,纯粹的python?

KeyError: "Does not support option: 'parallel'" 

,所以我决定测试nogil如果解除从我的CPU整体的能力,但它比纯Python我不明白为什么这个慢事情发生了,如果有人可以帮助我,或指引,我将非常感激

import numpy as np 
from numba import * 
@jit(['float64[:,:],float64[:,:]'],'(n,m),(n,m)->(n,m)',nogil=True) 
def asd(x,y): 
    return x+y 
u=np.random.random(100) 
w=np.random.random(100) 

%timeit asd(u,w) 
%timeit u+w 

10000圈,最好的3:每循环 137微秒最慢的运行速度比最快的耗时较长7.13倍。这可能意味着正在缓存中间结果 1000000个循环,最好为3:每个循环1.75μs

回答

3

您不能指望numba在这种简单的矢量化操作上胜过numpy。由于numba函数包含外部函数调用的成本,因此您的比较并不完全公平。如果总结更大的阵列,你会发现两者的性能汇聚,哪些是你所看到的仅仅是在一个非常快的操作开销:

import numpy as np 
import numba as nb 

@nb.njit 
def asd(x,y): 
    return x+y 

def asd2(x, y): 
    return x + y 

u=np.random.random(10000) 
w=np.random.random(10000) 

%timeit asd(u,w) 
%timeit asd2(u,w) 

The slowest run took 17796.43 times longer than the fastest. This could mean 
that an intermediate result is being cached. 
100000 loops, best of 3: 6.06 µs per loop 

The slowest run took 29.94 times longer than the fastest. This could mean that 
an intermediate result is being cached. 
100000 loops, best of 3: 5.11 µs per loop 

至于并行功能,对于这个简单的操作,再次

@nb.vectorize([nb.float64(nb.float64, nb.float64)], target='parallel') 
def asd3(x, y): 
    return x + y 

u=np.random.random((100000, 10)) 
w=np.random.random((100000, 10)) 

%timeit asd(u,w) 
%timeit asd2(u,w) 
%timeit asd3(u,w) 

但是,如果你在小数组操作,你将要看到线程分派的开销:您可以使用nb.vectorize。对于上面的数组大小,我看到并行给我一个2x加速。

凡numba真正的闪光点是这样做很难用广播在numpy的做,或者在操作会导致大量的临时中间阵列分配的操作。

+0

嗨@JoshAdel感谢您的回答,我真正的代码是algorith所以它遍历相同的功能,检查改进,所以我不知道如果优化的aguments值的不断变化和嵌套函数量如果我矢量化执行最多的函数将会是一个问题。再次感谢你 – jmparejaz