Issue 6370: Performance issue with collections.Counter().

This commit is contained in:
Raymond Hettinger 2009-06-29 19:10:29 +00:00
parent 34116922d3
commit 5dfc7f9fc8
1 changed files with 4 additions and 2 deletions

View File

@ -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)