I have a few dictionaries containing similar data.
Most queries would be resolved in a single search of one dictionary.
So is it better performance wise to leave out preliminary checks for existence of a key in a dict and have a try at the next dict in an except clause catching key error?
Or maybe something like
# d1, d2, d3 = bunch of dictionaries
value = d1.get(key, d2.get(key, d3.get(key, 0)))
?
Since there may be lots of empty spaces we'll be traversing on without any real benefits and hence Dictionaries are generally slower than their array/list counterparts. For large-sized Datasets, memory access would be the bottleneck.
Here are some disadvantages of using a Python dictionary. (i) Dictionaries are unordered. In cases where the order of the data is important, the Python dictionary is not appropriate. (ii) Python dictionaries take up a lot more space than other data structures.
Lookups are faster in dictionaries because Python implements them using hash tables. If we explain the difference by Big O concepts, dictionaries have constant time complexity, O(1) while lists have linear time complexity, O(n).
Python has a built-in function called type() that helps you find the class type of the variable given as input. Python has a built-in function called isinstance() that compares the value with the type given. If the value and type given matches it will return true otherwise false.
It seems in almost all cases, using get
would be faster. Here is my test run using try..except
and get
>>> def foo1(n):
spam = dict(zip(range(-99,100,n),[1]*200))
s = 0
for e in range(1,100):
try:
s += spam[e]
except KeyError:
try:
s += spam[-e]
except KeyError:
s += 0
return s
>>> def foo2(n):
spam = dict(zip(range(-99,100,n),[1]*200))
s = 0
for e in range(1,100):
s += spam.get(e, spam.get(-e,0))
return s
>>> for i in range(1,201,10):
res1 = timeit.timeit('foo1({})'.format(i), setup = "from __main__ import foo1", number=1000)
res2 = timeit.timeit('foo2({})'.format(i), setup = "from __main__ import foo2", number=1000)
print "{:^5}{:10.5}{:10.5}{:^10}{:^10}".format(i,res1,res2,foo1(i),foo2(i))
1 0.075102 0.082862 99 99
11 0.25096 0.054272 9 9
21 0.2885 0.051398 10 10
31 0.26211 0.060171 7 7
41 0.26653 0.053595 5 5
51 0.2609 0.052511 4 4
61 0.2686 0.052792 4 4
71 0.26645 0.049901 3 3
81 0.26351 0.051275 3 3
91 0.26939 0.051192 3 3
101 0.264 0.049924 2 2
111 0.2648 0.049875 2 2
121 0.26644 0.049151 2 2
131 0.26417 0.048806 2 2
141 0.26418 0.050543 2 2
151 0.26585 0.049787 2 2
161 0.26663 0.051136 2 2
171 0.26549 0.048601 2 2
181 0.26425 0.050964 2 2
191 0.2648 0.048734 2 2
>>>
Depends on the keys in the dictionaries.
If you predict with confidence that it is more common for keys to be missing use then use get.
If you predict with confidence that it is more common for keys to be there use try except.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With