Help optimize a script?
Joseph Santaniello
joseph at arbitrary.org
Wed Oct 17 15:27:52 EDT 2001
Thanks for the tips. These are going to make a big difference, I'm sure.
Joseph
PS: I'll change my anti-spam strategy. Since my email address is cryptic
enough as it is, I should make it more clear how to change it to make it
valid.
On Wed, 17 Oct 2001 at 13:20, Skip Montanaro wrote:
>
> 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.
>
>
More information about the Python-list
mailing list