Skip to content

Instantly share code, notes, and snippets.

@dpifke
Created March 29, 2012 23:32
Show Gist options
  • 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 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