Can you do it faster? (parsing text file)

Paul Rubin phr-n2003b at NOSPAMnightsong.com
Tue Jan 28 09:34:42 EST 2003


> The replace operations are done with the whole file instead of 
> for li in fa.readlines():
> because I think it's faster.

I have some doubts about this because of all the characters that
have to bemoved around, but it's possible.

> Is there a differnt approach for parsing such a file?

It seems to me you have to time the different phases and see what
needs tuning.  Here's a small class I like to use for that kind of thing:

    from time import time

    class timer:
        def __init__(self):
            self.t0 = time()
            self.t1 = self.t0
        def __call__(self,msg=''):
            t=time()
            print "%s: delta=%.5f sec total=%.5f sec"%  \
                  (msg, t-self.t1, t-self.t0)
            self.t1 = t

Then at the start I say
   t = timer()

and then sprinkle calls like

   t("finished deleting spaces")

through the program to get messages it executes.  Each time you
call t, it prints the number of seconds since the last call, and
the number of seconds since t was first initialized.




More information about the Python-list mailing list