Can you make this faster?

Kamilche klachemin at home.com
Sun Jun 27 14:22:18 EDT 2004


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.

def fmtstring(args):
    delim = '\0'
    fmt = []
    fmt.append('<')
    for arg in args:
        t = type(arg)
        if t == types.StringType:
            l = len(arg)
            fmt.append(str(l) + 's')
        elif t == types.IntType:
            fmt.append('i')
        elif t == types.LongType:
            fmt.append('q')
        elif t == types.BooleanType:
            fmt.append('c')
        elif t == types.FloatType:
            fmt.append('d')
        else:
            raise Exception("Can't pack argument of type %s!" % t)
    s = ''.join(fmt)
    s = s + '\0'
    return s



More information about the Python-list mailing list