Help optimize a script?

Skip Montanaro skip at pobox.com
Wed Oct 17 14:20:06 EDT 2001


    Joseph> First, does anyone know of a tool that does this so I don't have
    Joseph> to reinvent the wheel, and barring that, can anyone offer some
    Joseph> tips on how to optimize this code
    [snip]
    Joseph> Any suggestions on how to speed this up?

First suggestion: check out the xreadlines module if you are using a pre-2.2
version of Python.  If you are using 2.2 already you can just loop over the
file object because it implements the iterator protocol:

    for line in sys.stdin:
        ...

(xreadlines might still be faster because of buffering, you'll have to
check to be sure.)

Second suggestion: make "new" a list and either just append to it (starting
with new being an empty list):

     new = []
     for index ...:
         new.append(string.strip(...))
         ...

or set the index in new explicitly (starting with new being a list of
appropriate length filled with None):

    new = [None] * len(indecies[sys.argv[1]])
    for index ...:
        new[index] = string.strip(...))
        ...

When you want to print the result just join the list:

    print "".join(new)

or

    print string.join(new, "")

Third (minor) suggestion: if you are using a version of Python with string
methods (to check, does executing "print ''.join" work?), skip string.strip
and just use the strip() method of the string you're manipulating.

Fourth suggestion: to allow people to respond directly to you, don't try and
disguise your email address.  I couldn't tell what I needed to do to get
email to you (delete "no-spam"?  change "someone" to something else? change
"arbitrary" to something else?), so I just punted and am simply replying to
the list.

-- 
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list