Can you make this faster?

alejandro david weil aweil at mail.ru
Mon Jun 28 16:37:23 EDT 2004


On Mon June 28 2004 16:51, Andrea Griffini wrote:
> After making a few experiments I found this faster...
>
> def fmtstring5(args,
[..]
>         t = _type(arg)
>         if t is _str:
>             l = _len(arg)
>             fmt = fmt + _str(_len(arg)) + 's'
>         elif t is _int:
[..]
>         else:
>             raise Exception("Can't pack argument of type %s!" % t)
>     return fmt+'\0'

> PS: I'm new to python, and found its speed interesting and
>     more than adequate for many uses... It's however kind
>     of depressing seeing that so much dinamicity is "wasted"
>     in places where it's not needed (e.g. when calling "len"
>     or "str"). This version was running on the examples I
>     tested about twice the speed of the original version.

It seems that no one has read my last post :-)

You can replace str() with an array access for many of the strings as you want..
let's say..

slen = []
for i in xrange(256):
	slen.append(str(i))

And then, in your function:

def mystrbla..():
	...
	try:
		fmt += slen[_len(arg)] + 's'
	except:
		fmt += str(_len(arg)) + 's'
Please, try it! :-)

------------------------------------------------------------------

And, another improvment is add the 's' to the 
slen array :-) and..:

slen = []
for i in xrange(256):
	slen.append(str(i)+'s')

def mystrbla..():
	...
	l = _len(arg)
	if l <256:
		fmt += slen[l] + 's'
	except:
		fmt += str(l) + 's'


Unreaded greetings!
alejandro
-- 
+ There is no dark side of the moon really. Matter of fact it's all dark.





More information about the Python-list mailing list