是否有与以下功能等效的标准库/ numpy:
def augmented_assignment_sum(iterable, start=0):
for n in iterable:
start += n
return start
?
虽然sum(ITERABLE)
非常优雅,但它使用+
operator代替+=
,如果出现np.ndarray
对象,这可能会影响性能。
我已经测试过,我的功能可能和它一样快sum()
(而使用它的等效+
速度要慢得多)。由于它是纯Python函数,因此我认为它的性能仍然受到限制,因此我正在寻找一些替代方法:
In [49]: ARRAYS = [np.random.random((1000000)) for _ in range(100)]
In [50]: def not_augmented_assignment_sum(iterable, start=0):
...: for n in iterable:
...: start = start + n
...: return start
...:
In [51]: %timeit not_augmented_assignment_sum(ARRAYS)
63.6 ms ± 8.88 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [52]: %timeit sum(ARRAYS)
31.2 ms ± 2.18 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [53]: %timeit augmented_assignment_sum(ARRAYS)
31.2 ms ± 4.73 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [54]: %timeit not_augmented_assignment_sum(ARRAYS)
62.5 ms ± 12.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [55]: %timeit sum(ARRAYS)
37 ms ± 9.51 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [56]: %timeit augmented_assignment_sum(ARRAYS)
27.7 ms ± 2.53 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
我尝试将其functools.reduce
与结合使用operator.iadd
,但其性能相似:
In [79]: %timeit reduce(iadd, ARRAYS, 0) 33.4 ms ± 11.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) In [80]: %timeit reduce(iadd, ARRAYS, 0) 29.4 ms ± 2.31 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
我对内存效率也很感兴趣,因此更喜欢扩展分配,因为它们不需要创建中间对象。