Commit f909202c authored by Raymond Hettinger's avatar Raymond Hettinger

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

parent 0156f917
...@@ -421,13 +421,15 @@ class Counter(dict): ...@@ -421,13 +421,15 @@ class Counter(dict):
if iterable is not None: if iterable is not None:
if isinstance(iterable, Mapping): if isinstance(iterable, Mapping):
if self: if self:
self_get = self.get
for elem, count in iterable.items(): for elem, count in iterable.items():
self[elem] += count self[elem] = count + self_get(elem, 0)
else: else:
dict.update(self, iterable) # fast path when counter is empty dict.update(self, iterable) # fast path when counter is empty
else: else:
self_get = self.get
for elem in iterable: for elem in iterable:
self[elem] += 1 self[elem] = 1 + self_get(elem, 0)
if kwds: if kwds:
self.update(kwds) self.update(kwds)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment