Hi Pythonistas
Today we are going to analyze the speed comparison of different ways of merging dict. Let us see with examples
a = {str(i): i for i in range(100)}
b = {str(i): i for i in range(101, 200)}
Using update method of dict
def using_update():
c = a.copy()
c.update(b)
return c
Using merge operator
def using_merge_operator():
return a | b
Using unpacking
def using_unpacking():
return { **a, **b }
Using chain map
from collections import ChainMap
def using_chainmap():
return ChainMap(a, b)
Using itertools chain operator
def using_chain():
return itertools.chain(a.items(), b.items())
Analysis using timeit module
time_taken = timeit.timeit(using_update, number=1000_000)
print("Total time for using_update", time_taken)
time_taken = timeit.timeit(using_merge_operator, number=1000_000)
print("Total time for using merge", time_taken)
time_taken = timeit.timeit(using_unpacking, number=1000_000)
print("Total time for using unpacking", time_taken)
time_taken = timeit.timeit(using_chainmap, number=1000_000)
print("Total time for using chainmap", time_taken)
time_taken = timeit.timeit(using_chain, number=1000_000)
print("Total time for using chain", time_taken)
Output
Total time for using_update 2.3451301979985146
Total time for using merge 2.228314269999828
Total time for using unpacking 2.357389224001963
Total time for using chainmap 0.24351109200142673
Total time for using chain 0.17889608000041335
We can see that the chain method and chaimap are much faster compared to dictionary operations. Issue with chain operator it returns an iterable we cannot access using the key.
But in the case of chainmap it takes less time and can be accessed using a key.
Hope you have learned something from this post. Please share your suggestions to afsal@parseltongue.co.in