From 5dfc7f9fc8b0a7eda357643234e415ff51e79711 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 29 Jun 2009 19:10:29 +0000 Subject: [PATCH] Issue 6370: Performance issue with collections.Counter(). --- Lib/collections.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/collections.py b/Lib/collections.py index 1e807af81dc..abf6f8927d9 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)