I noticed that when I use user-defined objects (that override the __hash__
method) as keys of my dicts in Python, lookup time increases by at least a factor 5.
This behaviour is observed even when I use very basic hash methods such as in the following example:
class A:
def __init__(self, a):
self.a = a
def __hash__(self):
return hash(self.a)
def __eq__(self, other):
if not isinstance(other, A):
return NotImplemented
return (self.a == other.a and self.__class__ ==
other.__class__)
# get an instance of class A
mya = A(42)
# define dict
d1={mya:[1,2], 'foo':[3,4]}
If I time the access through the two different keys I observe a significant difference in performance
%timeit d1['foo']
results in ~ 100 ns. Whereas
%timeit d1[mya]
results in ~ 600 ns.
If I remove the overwriting of __hash__
and __eq__
methods, performance is at the same level as for a default object
Is there a way to avoid this loss in performance and still implement a customised hash calculation ?