String Identity Test

Christian Heimes lists at cheimes.de
Wed Mar 4 04:33:03 EST 2009


Avetis KAZARIAN schrieb:
> Gary Herron wrote:
>> The question now is:  Why do you care?   The properties of strings do
>> not depend on the implementation's choice, so you shouldn't care because
>> of programming considerations.  Perhaps it's just a matter of curiosity
>> on your part.
>>
>> Gary Herron
> 
> Well, it's not about curiosity, it's more about performance.
> 
> I will make a PHP example (a really quite simple )
> 
> PHP :
> 
> Stat 1 : $aVeryLongString == $anOtherVeryLongString
> Stat 2 : $aVeryLongString === $anOtherVeryLongString
> 
> Stat 2 is really faster than Stat 1 (due to the binary comparison)
> 
> As I said, I'm coming from PHP, so I was wondering if there was such a
> difference in Python.

Python uses some tricks to speed up string comparison. The struct of the
string type contains the length of the string and it caches the hash of
the string, too.

s1 == s2 is broken down to several steps. Here is the Python equivalent
of the C code:

# for strings, identity is always equality
if s1 is s2:
    return True

# compare the size
if len(s1) != len(s2):
    return False

# special case strings with a length of one
if len(s1) == 1 and s1[0] == s2[0]:
    return True

# compare the hash
if hash(s1) != hash(s2):
    return False

# if size and hash are equal compare every char* of the str
for i in xrange(len(s1)):
    if s1[i] != s2[i]:
        return False

# it's really the same thing
return True

Christian




More information about the Python-list mailing list