String Identity Test
tino at wildenhain.de
Wed Mar 4 10:32:58 CET 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.
Please keep in mind in both cases there is nothing "for free".
To have identity, you would need to have the same object - which
in case of a string means the interpreter has to find out about
existing string with exactly the same contents and reference it
instead of creating a new object in memory. This takes about at least
the same time (if not more) then just run the compare with both strings
when you need (aka == ).
If you only have a few strings but compare them often, you could
profit from identity and the overhead of installing it would
be neglectable (and you can force this in python with "internal")
but in this case I'd think calculating and working with a hash
instead should be preferred.
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 3241 bytes
Desc: S/MIME Cryptographic Signature
More information about the Python-list