From: Raymond Hettinger Date: Mon, 29 Jun 2009 19:10:29 +0000 (+0000) Subject: Issue 6370: Performance issue with collections.Counter(). X-Git-Tag: v2.7a1~874 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5dfc7f9fc8b0a7eda357643234e415ff51e79711;p=python Issue 6370: Performance issue with collections.Counter(). --- diff --git a/Lib/collections.py b/Lib/collections.py index 1e807af81d..abf6f8927d 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -418,13 +418,15 @@ class Counter(dict): if iterable is not None: if isinstance(iterable, Mapping): if self: + self_get = self.get for elem, count in iterable.iteritems(): - self[elem] += count + self[elem] = self_get(elem, 0) + count else: dict.update(self, iterable) # fast path when counter is empty else: + self_get = self.get for elem in iterable: - self[elem] += 1 + self[elem] = self_get(elem, 0) + 1 if kwds: self.update(kwds)