Minor improvements to the convolve() recipe (GH-24012)
https://github.com/python/cpython/commit/f421bfce80730cb0ff5cbe14727ac30cf04... commit: f421bfce80730cb0ff5cbe14727ac30cf0462eed branch: master author: Raymond Hettinger <rhettinger@users.noreply.github.com> committer: rhettinger <rhettinger@users.noreply.github.com> date: 2020-12-30T12:51:19-08:00 summary: Minor improvements to the convolve() recipe (GH-24012) * Minor improvement to speed and space efficiency for the convolve() recipe * Don't require convolve's kernel to be a sequence. files: M Doc/library/itertools.rst diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 03416a0a7fbcb..fd77f99a88f57 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -791,9 +791,9 @@ which incur interpreter overhead. # convolve(data, [0.25, 0.25, 0.25, 0.25]) --> Moving average (blur) # convolve(data, [1, -1]) --> 1st finite difference (1st derivative) # convolve(data, [1, -2, 1]) --> 2nd finite difference (2nd derivative) - kernel = list(reversed(kernel)) + kernel = tuple(kernel)[::-1] n = len(kernel) - window = collections.deque([0] * n, maxlen=n) + window = collections.deque([0], maxlen=n) * n for x in chain(signal, repeat(0, n-1)): window.append(x) yield sum(map(operator.mul, kernel, window))
participants (1)
-
rhettinger