Say you want to optimize a (byte) string compare intensive algorithm implemented in Python. Since a central code path contains this sequence of statements
if s < t:
# less than ...
elif t < s:
# greater than ...
else:
# equal ...
it would be great to optimize it to something like
r = bytes_compare(s, t)
if r < 0:
# less than ...
elif r > 0:
# greater than ...
else:
# equal ...
where (the hypothetical) bytes_compare() ideally would just call the three-way comparison C function memcmp() which is usually quite well optimized. This would reduce the number of string comparisons by half. A very feasible optimization unless the strings are ultra short.
But how to get there with Python 3?
PS:
Python 3 has removed the three way comparison global function cmp() and the magic method __cmp__(). And even with Python 2, the bytes class doesn't had a __cmp__() member.
With the ctypes package it's straight forward to call memcmp() but the foreign function call overhead with ctypes is prohibitively high.
s < tcomparison implemented atm, given thatsandtarebytes?swithtfollowed bytwiths). Note that this pattern isn't unusual - for example, you also have this when you merge/join 2 sorted sequences of strings. Also, Python string comparison aren't slow in general - if you just need one comparison outcome for a pairsandtthen they are fast, as is. Thus, this really isn't about isolated microbenchmarking.Objects/bytesobject.cand it callsmemcmp()inbytes_compare_eq()andbytes_richcompare()(for<or>).cmp()in python 3, the suggested way is to do:((a > b) - (b > a)). This will give you therbut ... it is still 2 comparisons. I have tried a python-based implementation ofbytes_compare... very bad idea :) at least 3x slower