是否有与以下功能等效的标准库/numpy:
def augmented_assignment_sum(iterable, start=0):
for n in iterable:
start += n
return start
?
虽然sum(ITERABLE)
非常优雅,但它使用+
运算符而不是+=
,这在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)
我也对内存效率感兴趣,因此更喜欢增强分配,因为它们不需要创建中间对象。