Comparing strings from the back?

Terry Reedy tjreedy at udel.edu
Tue Sep 4 01:13:18 EDT 2012


On 9/3/2012 9:54 PM, Roy Smith wrote:
> There's been a bunch of threads lately about string implementations, and
> that got me thinking (which is often a dangerous thing).
>
> Let's assume you're testing two strings for equality.  You've already
> done the obvious quick tests (i.e they're the same length), and you're
> down to the O(n) part of comparing every character.
>
> I'm wondering if it might be faster to start at the ends of the strings
> instead of at the beginning?

I posted the 3.3 str (in)equality compare a few days ago. It first 
compares length and then kind, then the actual bytes in whatever chunks. 
If the latter uses the C/system mem compare, that is faster than 
anything coded in Python or even C.

-- 
Terry Jan Reedy




More information about the Python-list mailing list