Skip to content

Instantly share code, notes, and snippets.

@dpifke
Created March 29, 2012 23:32
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dpifke/2244911 to your computer and use it in GitHub Desktop.
Save dpifke/2244911 to your computer and use it in GitHub Desktop.
Performance comparison of defaultdict vs. Counter and tuple vs. namedtuple in Python
#!/usr/bin/python
#
# Copyright (c) 2012 Dave Pifke.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# This is a simple performance test of different methods for counting the
# number of occurrences of a series of values.
def values():
"""
Returns a tuple containing four random values: an integer between 0 and
512, a boolean, an integer between 0 and 256, and a boolean, respectively.
"""
from random import randint
return (randint(0, 512),
bool(randint(0, 1)),
randint(0, 256),
bool(randint(0 , 1)))
def nested_defaultdict(n):
"""
Returns a series of nested defaultdict objects, four deep. The value of
the innermost dict is the number of occurrences of the keys that got us
there.
"""
from collections import defaultdict
from functools import partial
counts = defaultdict(
partial(defaultdict,
partial(defaultdict,
partial(defaultdict, int))))
for i in range(n):
a, b, c, d = values()
counts[a][b][c][d] += 1
return counts
def tuple_defaultdict(n):
"""
Returns a defaultdict where the key is a tuple of the input values and
the value is the number of occurrences.
"""
from collections import defaultdict
counts = defaultdict(int)
for i in range(n):
a, b, c, d = values()
counts[(a, b, c, d)] += 1
return counts
def namedtuple_defaultdict(n):
"""
Returns a defaultdict where the key is a namedtuple of the input values and
the value is the number of occurrences.
"""
from collections import namedtuple, defaultdict
counts = defaultdict(int)
Key = namedtuple('Key', 'a b c d')
for i in range(n):
a, b, c, d = values()
counts[Key(a, b, c, d)] += 1
return counts
def tuple_counter_update(n):
"""
Returns a Counter, keyed using a tuple. Uses Counter.update().
"""
from collections import Counter
counts = Counter()
for i in range(n):
a, b, c, d = values()
counts.update((a, b, c, d))
return counts
def tuple_counter_incr(n):
"""
Returns a Counter, keyed using a tuple. Uses Counter()[value] += 1.
"""
from collections import Counter
counts = Counter()
for i in range(n):
a, b, c, d = values()
counts[(a, b, c, d)] += 1
return counts
def namedtuple_counter_update(n):
"""
Returns a Counter, keyed using a namedtuple. Uses Counter.update()
"""
from collections import namedtuple, Counter
counts = Counter()
Key = namedtuple('Key', 'a b c d')
for i in range(n):
a, b, c, d = values()
counts.update(Key(a, b, c, d))
return counts
def namedtuple_counter_incr(n):
"""
Returns a Counter, keyed using a namedtuple. Uses Counter()[value] += 1.
"""
from collections import namedtuple, Counter
counts = Counter()
Key = namedtuple('Key', 'a b c d')
for i in range(n):
a, b, c, d = values()
counts[Key(a, b, c, d)] += 1
return counts
if __name__ == '__main__':
from timeit import Timer
funcs = [nested_defaultdict,
tuple_defaultdict,
namedtuple_defaultdict,
tuple_counter_update,
tuple_counter_incr,
namedtuple_counter_update,
namedtuple_counter_incr]
# Credit to Raymond Hettinger for the following:
setup = 'from __main__ import %s' % ', '.join([x.__name__ for x in funcs])
for func in funcs:
stmt = '%s(%d)' % (func.__name__, 1000)
print(func.__name__, min(Timer(stmt, setup).repeat(7, 20)))
# eof
@dpifke
Copy link
Author

dpifke commented Mar 29, 2012

Using CPython, tuples are slightly more efficient than nesting and namedtuples, and Counter is significantly slower than defaultdict:

dave@anarchy:~$ python --version
Python 2.7.2+
dave@anarchy:~$ python counter_test.py
nested_defaultdict 0.205599069595
tuple_defaultdict 0.188335895538
namedtuple_defaultdict 0.219454050064
tuple_counter 0.295751094818
namedtuple_counter 0.336565971375

Using PyPy, namedtuples perform significantly worse than regular tuples, but Counter and defaultdict are about equivalent:

dave@anarchy:~$ pypy --version
Python 2.7.2 (2346207d99463f299f09f3e151c9d5fa9158f71b, Feb 15 2012, 17:02:59)
[PyPy 1.8.0 with GCC 4.6.1]
dave@anarchy:~$ pypy counter_test.py
nested_defaultdict 0.031476020813
tuple_defaultdict 0.0274238586426
namedtuple_defaultdict 0.159631967545
tuple_counter 0.0281150341034
namedtuple_counter 0.686085939407

Interesting that both the best and the worst results are using PyPy.

@dpifke
Copy link
Author

dpifke commented Mar 30, 2012

...and using CPython 3.2 (after making some syntax changes):

dave@anarchy:~$ python3 --version
Python 3.2.2
dave@anarchy:~$ python3 counter_test.py
nested_defaultdict 0.29567694664001465
tuple_defaultdict 0.2673780918121338
namedtuple_defaultdict 0.3005399703979492
tuple_counter 0.3803229331970215
namedtuple_counter 0.4219701290130615

Not sure why this is slower than 2.7 across the board, but it is.

@dpifke
Copy link
Author

dpifke commented Mar 30, 2012

Thinking that perhaps Counter.update() versus counts[value] += 1 was an unfair comparison, I added timings for each. New results:

dave@anarchy:~$ python counter_test.py
('nested_defaultdict', 0.20764708518981934)
('tuple_defaultdict', 0.18820714950561523)
('namedtuple_defaultdict', 0.21594595909118652)
('tuple_counter_update', 0.29559993743896484)
('tuple_counter_incr', 0.1953890323638916)
('namedtuple_counter_update', 0.33496999740600586)
('namedtuple_counter_incr', 0.22653508186340332)
dave@anarchy:~$ pypy counter_test.py
('nested_defaultdict', 0.032904863357543945)
('tuple_defaultdict', 0.02780008316040039)
('namedtuple_defaultdict', 0.1599569320678711)
('tuple_counter_update', 0.02878713607788086)
('tuple_counter_incr', 0.021777868270874023)
('namedtuple_counter_update', 0.6833841800689697)
('namedtuple_counter_incr', 0.14212298393249512)
dave@anarchy:~$ python3 counter_test.py
nested_defaultdict 0.3038370609283447
tuple_defaultdict 0.27263712882995605
namedtuple_defaultdict 0.3053758144378662
tuple_counter_update 0.38199901580810547
tuple_counter_incr 0.28246116638183594
namedtuple_counter_update 0.4246039390563965
namedtuple_counter_incr 0.31453800201416016

update() is slower across the board, which seems counter-intuitive.

@pswaminathan
Copy link

Hi, thanks for performing these benchmarks. A Google search for performance of python defaultdict vs counter led me here.

Out of curiosity, why did you choose to import defaultdict and Counter in the function instead of using it as a setup? I'd be curious to see what the differences there are, as importing them as a setup rather than as part of the function could have a significant impact on both the overall times and the relative times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment