collections.Counter surprisingly slow

Serhiy Storchaka storchaka at
Tue Jul 30 15:04:12 CEST 2013

29.07.13 14:49, Joshua Landau написав(ла):
> I find it hard to agree that counter should be optimised for the
> unique-data case, as surely it's much more oft used when there's a point
> to counting?

Different methods are faster for different data. LBYL approach is best 
for the mostly unique data case, while EAFP approach is best for the 
mostly repeated data case. In general case a performance of particular 
method is a function of its performances in this two extreme cases. When 
it slow for one of extreme case it can be slow in a number of 
intermediate cases.

> Also, couldn't Counter just extend from defaultdict?

Unfortunately this only will slowdown it.

More information about the Python-list mailing list