Comparing a memoryview to itself compares all values which is slow (O(n) complexity). Comparing a bytes string to itself returns True without comparing each byte.
from timeit import timeit with open("/dev/random", 'br') as fp: data = fp.read(2**20) view = memoryview(data) LOOPS = 1_000 b = timeit('x == x', number=LOOPS, globals={'x': data}) m = timeit('x == x', number=LOOPS, globals={'x': view}) print("bytes %f seconds" % b) print("mview %f seconds" % m) print("โ %f time slower" % (m / b))
Output on Python 3.14:
bytes 0.000027 seconds
mview 2.663470 seconds
โ 97806.240268 time slower
I propose to implement the same optimization than bytes in memoryview: return True immediately when comparing a memoryview to itself.
We should just pay attention to float types which cannot use the optimization. Floats can be equal to NaN (Not-a-Number) and NaN is not equal to itself.