Can you make this faster?
Ype Kingma
ykingma at accessforall.nl
Mon Jun 28 13:30:22 EDT 2004
alejandro david weil wrote:
> On Sun June 27 2004 15:22, 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.
>>
...
>
> But, anyway, the problem seems to be the:
> f2 += str(len(arg))
> line.
You can try this (untested):
def myfmtstring(args):
global typedic
f2l = ['<']
t = None
for arg in args:
t = type(arg)
if t == types.StringType:
f2l.append(str(len(arg)))
try:
f2l.append(typedic[t])
except: #check the exception here!
raise Exception("Can't pack argument of type %s!" % t)
fl2.append('\0')
return ''.join(f2l)
> If you take it off, you'll see that time reduces to half!
> Why? How to fix?
Appending to a string copies the original completely.
Good luck,
Ype
--
email at xs4all.nl
More information about the Python-list
mailing list