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.StringTyp e:
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.BooleanTy pe:
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
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.StringTyp e:
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.BooleanTy pe:
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
Comment