Python why
>>> b"A" in b"ABC"
True
>>> ord(b"A") in b"ABC"
True
>>> b"A" in memoryview(b"ABC")
False
>>> ord(b"A") in memoryview(b"ABC")
True
Python why
>>> b"A" in b"ABC"
True
>>> ord(b"A") in b"ABC"
True
>>> b"A" in memoryview(b"ABC")
False
>>> ord(b"A") in memoryview(b"ABC")
True
memoryview(b"ABC")[0] is__contains__ method, memoryview doesn't and so the in operator ends up iterating through memoryview and doing literal comparisons.
Sometimes profile results can be surprising but looking at the code the orders of magnitude timing difference is hardly surprising. It turns every byte one-by-one into a heap-allocated Python object just to then compare it with another Python object and deallocate. If you wanted to make this faster you would convert the input to a C type at the start and then do all the comparisons with C types. That would not be difficult but just needs more code to handle the different possible input types an...