Can you make this faster?

Andrea Griffini agriff at tin.it
Mon Jun 28 15:51:30 EDT 2004


On 27 Jun 2004 11:22:18 -0700, klachemin at home.com (Kamilche) wrote:

>I have a routine that I really need, but it slows down processing
>significantly. Can you spot any ineffeciencies in the code?
>
>This code makes a critical function of mine run about 7x slower than
>using a prebuilt format string. For maximum flexibility, it would be
>best to calculate the format string using this method, so I'd dearly
>love to keep it.

After making a few experiments I found this faster...

def fmtstring5(args,
               _type = type,
               _len = len,
               _str = str,
               _int = int,
               _long = long,
               _bool = bool,
               _float = float):
    fmt = "<"
    for arg in args:
        t = _type(arg)
        if t is _str:
            l = _len(arg)
            fmt = fmt + _str(_len(arg)) + 's'
        elif t is _int:
            fmt = fmt + 'i'
        elif t is _long:
            fmt = fmt + 'q'
        elif t is _bool:
            fmt = fmt + 'c'
        elif t is _float:
            fmt = fmt + 'd'
        else:
            raise Exception("Can't pack argument of type %s!" % t)
    return fmt+'\0'

In other words I tried to remove dynamic lookups for predefined
functions and used "is int" instead of the "types." stuff.
s = s + x seems faster than s += x, and s = s + x seems faster
that append & "".join also (at least for small formats).

I tried to remove all setup cost (using a generator acting on
a global variable), but gained nothing (and indeed that was a
bit slower). I tried also to remove dynamic lookups for the
addition (concatenation), but in no case I found results better
than the plain vanilla addition syntax.

HTH
Andrea

PS: I'm new to python, and found its speed interesting and
    more than adequate for many uses... It's however kind
    of depressing seeing that so much dinamicity is "wasted"
    in places where it's not needed (e.g. when calling "len"
    or "str"). This version was running on the examples I
    tested about twice the speed of the original version.

    So I suppose that the same is happening all over my
    python code :-(




More information about the Python-list mailing list