哪个更快? 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
[OUT]:
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
答案 0 :(得分:2)
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()