Python speed-up

Jeff Epler jepler at unpythonic.net
Wed Sep 22 10:36:02 EDT 2004


The statements
    s += t
and
    s = s[j:]
(strings s and t; integer j) both take time related to the length of s.
The first creates a new string copies len(s)+len(t) characters into it,
and then assigns the new string to s.  The second creates a new string,
copies len(s)-j characters into it, and assigns the new string to s.

This is mostly a consequence of string immutability, though it's possible
that sneaky optimizations might be able to make these operations faster
in a future version of Python.

You might write the first loop as
    encoded_text = "".join([table[c] for c in original_text])
which can be written out as
    encoded_text_parts = []
    for c in original_text:
        encoded_text_parts.append(c)
    encoded_text = "".join(encoded_text_parts)
Accumulating string parts in a list and then joining them is one common
way to increase performance of code that repeatedly joins strings.

For the second, consider writing
    chr_list = [encoded_text[i:i+8] for i in range(0, len(encoded_text), 8)]
which can be written out as
    chr_list = []
    for i in range(0, len(encoded_text), 8):
        chr_list.append(encoded_text[i:i+8])
Instead of whittling away encoded_text, just take the 8 characters
you're interested in each time.

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040922/b298fa5a/attachment.sig>


More information about the Python-list mailing list