I have a 3-dimensional point class whose hash function, according to the profiler, would be a good place to do some optimizing. Right now, I’m just passing a tuple of the coordinates to the built-in hash function:
def __hash__(self):
return hash((self.x, self.y, self.z))
How can I make this faster? I’m assuming constructing a tuple each time isn’t good. The coordinates are real-valued.
Using a tuple instead of your own class will be much faster.
If you really want to write p.x instead of p[0] then you can make your class a subclass of tuple and have accessors. It will still be much faster than implementing your own tuple.