Peter Otten <__pete...@web.de>: > Marko Rauhamaa wrote: >> I see no point in CPython's rotation magic. > > Let's see: > > $ cat hashperf.py > class A(object): > __slots__ = ["_hash"] > > def __hash__(self): > return self._hash > > def no_magic(): > a = A() > a._hash = id(a) > return a > > def magic(): > a = A() > a._hash = id(a) >> 4 > return a > > $ python3 -m timeit -s 'from hashperf import magic, no_magic; s = > {no_magic() for _ in range(10**5)}' 'for x in s: x in s' > 10 loops, best of 3: 70.7 msec per loop > > $ python3 -m timeit -s 'from hashperf import magic, no_magic; s = {magic() > for _ in range(10**5)}' 'for x in s: x in s' > 10 loops, best of 3: 52.8 msec per loop > > "magic" wins this makeshift test. Other than that you're right ;)
That's interesting, but suggests there's something weird (~ suboptimal) going on with CPython's scrambling algorithm. Also, your performance test might yield completely different results on other Python implementations. Apart from uniqueness, there's no particular reason to prefer one __hash__() value over another as long as the interesting bits are inside the CPU's simple integer range. Marko -- https://mail.python.org/mailman/listinfo/python-list