哪个更快? Counter()+=Counter
或Counter.update(Counter)
?
为什么一个比另一个快?
我已经尝试了一些简单的分析,但我认为这不足以最终保存Counter+=Counter
比Counter.update(Counter)
快:
from collections import Counter
import time
x = Counter(['abc','def', 'abc'])
y = Counter(['xyz', 'xyz', 'uvw', 'abc'])
start = time.time()
x.update(y)
end = time.time() - start
print x
print 'update:', end
print
x = Counter(['abc','def', 'abc'])
start = time.time()
x+=y
end = time.time() - start
print x
print 'plus:', end
[出]:
Counter({'abc': 3, 'xyz': 2, 'def': 1, 'uvw': 1})
update: 4.48226928711e-05
Counter({'abc': 3, 'xyz': 2, 'def': 1, 'uvw': 1})
plus: 2.28881835938e-05
Counter.update()方法的设计是为了更快。__add__()方法做了更多的工作,因为它必须消除非负值:
# heart of the update() loop in Python 2:
for elem, count in iterable.iteritems():
self[elem] = self_get(elem, 0) + count
# heart of the __add__() loop in Python 2:
result = Counter()
for elem, count in self.items():
newcount = count + other[elem]
if newcount > 0:
result[elem] = newcount
for elem, count in other.items():
if elem not in self and count > 0:
result[elem] = count
return result
如您所见,__add__方法做了相当多的工作。在Python 3的后续版本中还有另一个不同之处,__iadd__()方法执行真正的就地更新,而__add__()方法创建一个新的计数器,然后赋值来替换旧的计数器:
def __iadd__(self, other):
for elem, count in other.items():
self[elem] += count
return self._keep_positive()