optimization question

Skip Montanaro skip at pobox.com
Mon Aug 12 23:56:07 EDT 2002


    ark> My understanding is that because strings are immutable, any Python
    ark> implementation is permitted to disregard object identity when it
    ark> computes string values.  So, if I write
    ark> 
    ark> a = "hello, world"
    ark> b = a[0:5]
    ark> 
    ark> the implementation is permitted to cause a and b to refer to two
    ark> different subsequences of the same part of memory.

In theory, I suppose that's true.  However, note that performance of such a
modified Python interpreter would probably suffer, because any time the
interpreter wanted to call a C routine which expected a null-terminated
string (e.g. strchr), it would have to be copied first (or at least
inspected using the stored length), since any string's (and b's in
particular) internal representation wouldn't necessarily be null-terminated.
CPython's current implementation always null-terminates strings to avoid
copying.

-- 
Skip Montanaro
skip at pobox.com
consulting: http://manatee.mojam.com/~skip/resume.html




More information about the Python-list mailing list