I was working on a piece of code which requires dictionary comparison, I am curious about the time complexity of it. How does python implement dictionary comparison?
if Dict_1 == Dict_2:
do something
I was working on a piece of code which requires dictionary comparison, I am curious about the time complexity of it. How does python implement dictionary comparison?
if Dict_1 == Dict_2:
do something
It is recursively compares pairs key:value
in dicts, so in theory the comparison time should be equal to number_of_all_recursive_items * time_to_check_one_item
(of course I am looking at the worst case - when dicts are equal and all pairs are checking). But in practice there is many pitfalls. Look at this picture:
It is the dict_length-time graph for flat dicts. Moreover, you can look at the comparison of the nested dict:
So you can't guarantee that your comparison will spend a particular amount of time. But usually - yes, there is a linear connection between recursive dict length and comparison time.