String Identity Test

Peter Otten __peter__ at web.de
Wed Mar 4 05:08:22 EST 2009


Avetis KAZARIAN wrote:

> 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.
> 
> Because I was trying to use "is" as for "===".

So you have two very long strings that may be equal. How did you get them?
If you read them from a file, that took much more time than the comparison. 

If they are sufficiently likely to be not equal just read them in smaller
chunks and compare these. If you want to compare multiple combinations use
hashes.

If 'a is b' worked like 'a == b' for arbitrary string that would mean that
the python implementation had done a lot of unnecessary 'a == b'
comparisons behind the scene or at least calculated a lot of hash values,
i. e. the ability to use the fast operation would in effect slow down your
program.

Peter



More information about the Python-list mailing list