-
-
Save macro1/f9232dd176c657abadf2 to your computer and use it in GitHub Desktop.
Test performance of different dictionary merging functions in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Results: | |
multiple_update: 92 ms | |
copy_and_update: 92 ms | |
dict_constructor: 93 ms | |
kwargs_hack: 93 ms | |
dict_comprehension: 91 ms | |
concatenate_items: 327 ms | |
union_items: 325 ms | |
chain_items: 203 ms | |
chainmap: 4 ms | |
dict_from_chainmap: 2447 ms | |
dict_unpacking: 91 ms | |
""" | |
import timeit | |
setup = """ | |
from collections import ChainMap | |
from itertools import chain, islice, product | |
def multiple_update(dict_a, dict_b): | |
merged = {} | |
merged.update(dict_a) | |
merged.update(dict_b) | |
return merged | |
def copy_and_update(dict_a, dict_b): | |
merged = dict_a.copy() | |
merged.update(dict_b) | |
return merged | |
def dict_constructor(dict_a, dict_b): | |
merged = dict(dict_a) | |
merged.update(dict_b) | |
return merged | |
def kwargs_hack(dict_a, dict_b): | |
return dict(dict_a, **dict_b) | |
def dict_comprehension(dict_a, dict_b): | |
return dict(dict_a, **dict_b) | |
def concatenate_items(dict_a, dict_b): | |
return dict(list(dict_a.items()) + list(dict_b.items())) | |
def union_items(dict_a, dict_b): | |
return dict(list(dict_a.items()) + list(dict_b.items())) | |
def chain_items(dict_a, dict_b): | |
return dict(chain(dict_a.items(), dict_b.items())) | |
def chainmap(dict_a, dict_b): | |
return ChainMap({}, dict_b, dict_a) | |
def dict_from_chainmap(dict_a, dict_b): | |
return dict(ChainMap(dict_b, dict_a)) | |
def dict_unpacking(dict_a, dict_b): | |
return {**dict_a, **dict_b} | |
allwords = [''.join(l) for l in islice(product('abcd', repeat=25), 500)] | |
third_idx = len(allwords) * 2 // 3 | |
defaults = dict.fromkeys(allwords[:third_idx]) | |
user = dict.fromkeys(allwords[third_idx:]) | |
""" | |
def time_function(func_name): | |
timed_code = """ | |
context = {}(defaults, user) | |
""".format(func_name).strip() | |
time = min(timeit.Timer(timed_code, setup=setup).repeat(7, 10000)) | |
print("{}: {} ms".format(func_name, int(time * 1000))) | |
time_function("multiple_update") | |
time_function("copy_and_update") | |
time_function("dict_constructor") | |
time_function("kwargs_hack") | |
time_function("dict_comprehension") | |
time_function("concatenate_items") | |
time_function("union_items") | |
time_function("chain_items") | |
time_function("chainmap") | |
time_function("dict_from_chainmap") | |
time_function("dict_unpacking") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment