from numba import njit@njitdef cumsum_breach_numba2(x, target, result): total = 0 iterID = 0 for i,x_i in enumerate(x): total += x_i if total >= target: result[iterID] = i iterID += 1 total = 0 return iterIDdef cumsum_breach_array_init(x, target): x = np.asarray(x) result = np.empty(len(x),dtype=np.uint64) idx = cumsum_breach_numba2(x, target, result) return result[:idx]
时机
包括
@piRSquared'ssolutions并使用同一篇文章中的基准测试设置-
In [58]: np.random.seed([3, 1415]) ...: x = np.random.randint(100, size=1000000).tolist()# @piRSquared soln1In [59]: %timeit list(cumsum_breach(x, 10))10 loops, best of 3: 73.2 ms per loop# @piRSquared soln2In [60]: %timeit cumsum_breach_numba(np.asarray(x), 10)10 loops, best of 3: 69.2 ms per loop# From this postIn [61]: %timeit cumsum_breach_array_init(x, 10)10 loops, best of 3: 39.1 ms per loop
Numba:追加与数组初始化
为了更仔细地了解数组初始化有何帮助,这似乎是两个numba实现之间的最大差异,让我们将它们放在数组数据上,因为数组数据的创建本身就很耗时,而且它们都依赖于它-
In [62]: x = np.array(x)In [63]: %timeit cumsum_breach_numba(x, 10)# with appending10 loops, best of 3: 31.5 ms per loopIn [64]: %timeit cumsum_breach_array_init(x, 10)1000 loops, best of 3: 1.8 ms per loop
为了强制输出拥有自己的存储空间,我们可以制作一个副本。虽然不会大幅度改变事情-
In [65]: %timeit cumsum_breach_array_init(x, 10).copy()100 loops, best of 3: 2.67 ms per loop
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)