[Tutor] re: Script too slow

Charlie Clark charlie@begeistert.org
Fri Feb 28 07:59:02 2003


On 2003-02-27 at 18:00:07 [+0100], tutor-request@python.org wrote:
> I have profiled the script using Python's profile utility. There is no 
> one function that slows down the script. Rather, each function seems to 
> take as much time. Here is an example:
> 
> 
>     def evaluate_token(self,token):
>         """Evaluate tokens. Return a value if the token is not a control 
>         word. Otherwise, pass token onto another method for further 
>         evaluation."""
>         if token == '{':
>             self.__bracket_count = self.__bracket_count + 1
>             num = '%04d' % self.__bracket_count
>             token = 'ob<nu<nu<nu<%(num)s<{\n' % vars()
>         elif token == '}':
>             num = '%04d' % self.__bracket_count
>             token = 'cb<nu<nu<nu<%(num)s<}\n' % vars()
>             self.__bracket_count = self.__bracket_count - 1
>         elif token == r'\{':
>             token = 'tx<es<nu<nu<nu<{\n'
>         elif token == r'\}':
>             token = 'tx<es<nu<nu<nu<}\n'
>         elif token == r'\\': # double or escaped \
>             token = 'tx<es<nu<nu<nu<\\\n'
>         elif token[0:1] != '\\': # single \
>             token = 'tx<nu<nu<nu<nu<%(token)s\n' % vars()
>         else:
>             token = self.evaluate_cw(token)

Paul,

I'm probably the last person who can really help you on this but there are 
a couple of things that strike me about this function.

First of all you can use self_bracket += 1 to get a bit more speed!!! Not 
much and I can't really tell you why but it is faster, it has something to 
do with only one value being used...

I don't know how you're getting your tokens but it looks this looks a lot 
like the stuff you write when using the HTML/SGML-parser. Are you hooked up 
to an RTF-Parser to do all the stuff? If not it might be worth looking at 
the SGMLParser code as to how it deals with <tag/> stuff - looks quite 
analogue to your "{", "}" stuff.

Python has a nioe way of avoiding long if, elif, ... else. You can use 
dictionaries.

ie.

d_test 0 = {"{":count+1, "}":count-1, r"\{":"tx"}
try:
	fn(d_test[token]) # some function which knows what to or use exec
except KeyError:
	other_fn(token)

that might be a little tricky to get right but should run faster, 
particularly for longer comparisons it's only one look-up per token.

Hope this is a little helpful.

Charlie