Can you make this faster?
alejandro david weil
aweil at mail.ru
Sun Jun 27 18:05:32 EDT 2004
On Sun June 27 2004 18:51, alejandro david weil wrote:
> > 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":
Well, that was not optimized and some debug print remains, this,
uses half time:
typedic = {
types.IntType: 'i',
types.LongType: 'q',
types.BooleanType: 'c',
types.FloatType:'d'}
slen = {}
for i in xrange(256):
slen[i]=str(i)+'s'
def myfmtstring(args):
global typedic, slen
f2 = '<'
t = None
for arg in args:
t = type(arg)
if t == types.StringType:
try:
f2+=slen[len(arg)]
except:
f2 += str(len(arg))+'s'
else:
try:
f2 += typedic[t]
except: #check the exception here!
raise Exception("Can't pack argument of type %s!" % t)
return f2+'\0'
-> Ran 2 tests in 0.253s
Also I tried the except-catch method proposed, in this way, and got ugly results:
(removing StringType from typedic)
def my_slowest_fmtstring(args):
global typedic, slen
f2 = '<'
t = None
for arg in args:
t = type(arg)
try:
f2 += typedic[t]
except: #check the exception here!
if t == types.StringType:
try:
f2+=slen[len(arg)]
except:
f2 += str(len(arg))+'s'
else:
raise Exception("Can't pack argument of type %s!" % t)
return f2+'\0'
-> Ran 2 tests in 0.632s (worst than the first version! that gives: Ran 2 tests in 0.465s)
--
+ There is no dark side of the moon really. Matter of fact it's all dark.
More information about the Python-list
mailing list