A common way of overriding the __hash__()
method for custom objects is to make a tuple t
with the object attributes and apply return hash(t)
, As described in this this question.
I have a program doing this, and after running cProfile, i've observed that this approach is the bottleneck of my program. I've made the following test and got the result:
>>> t1 = tm.Timer(setup="x = str((10,12))", stmt="hash(x)")
>>> t2 = tm.Timer(setup="x = (10,12)", stmt="hash(x)")
>>> min(t1.repeat(100)), min(t2.repeat(100))
(0.04663395881652832, 0.05523419380187988)
From which i conclude that using an string on the hash function is faster than
using a tuple. Given that i've made the __repr__()
implementation unique for every object of my class, i think that shouldn't be any problems.Before changing my program, is there any possible problem with this approach(or my test metodology)?