Can you make this faster?

alejandro david weil aweil at
Sun Jun 27 23:51:20 CEST 2004

> But, anyway, the problem seems to be the:
> 	f2 += str(len(arg))
> line.
> If you take it off, you'll see that time reduces to half!
> Why? How to fix?
> Don't know! :-(

For example, this seems to be "better":

typedic = {
    types.StringType : 's',
    types.IntType: 'i',
    types.LongType: 'q',
    types.BooleanType: 'c',

slen = {}
for i in xrange(256):

def myfmtstring(args):
    global typedic, slen
    f2 = '<'
    t = None
    for arg in args:
        t = type(arg)
        if t == types.StringType:
                print 'aca'
                f2 += str(len(arg))
            f2 += typedic[t]
        except: #check the exception here!
            raise Exception("Can't pack argument of type %s!" % t)
    return f2+'\0'

+ There is no dark side of the moon really. Matter of fact it's all dark.

More information about the Python-list mailing list