Re[2]: [Tutor] script still too slow

antonmuhin at rambler.ru antonmuhin at rambler.ru" <antonmuhin@rambler.ru
Fri Feb 28 14:15:02 2003


Hello Jeff,

Thursday, February 27, 2003, 10:39:49 PM, you wrote:

JS> This function, to my mind, screams out to be made into a dictionary
JS> lookup.  (Any time I'm dealing with a situation of "pick one option from 
JS> this list of possibilities", or what in C/C++ would be a switch/case, I 
JS> immediately think dictionary.)  If you put each of the given options 
JS> into a small one- to three-line function and put all of those functions 
JS> into a dictionary, you can then change this function into something like 
JS> the following:

JS>     def evaluate_token(self, token):
JS>         function = self.token_dict.get(token, self.evaluate_cw)
JS>         return function(self, token)  # passing in  self so that we can 
JS> access self.__bracket_count

JS> Or, almost equivalently but sparing the helper functions from needing to 
JS> know so much about the bracket count:

JS>     def evaluate_token(self, token):
JS>         function = self.token_dict.get(token, self.evaluate_cw)
JS>         token, self.__bracket_count = function(token, self.__bracket_count)
JS>         return token
I tried similar approach, but it was *slower*. I'm really surprised
and it might some subtle bug that slipped into my code.

JS> I also suspect that using the "formatstring % vars()" trick that you use
JS> is slower than simply making use of the actual variables in question. 
JS>  Try swapping, say, "token = 'tx<nu<nu<nu<nu<%(token)s\n' % vars()" with 
JS> "token = 'tx<nu<nu<nu<nu<%s\n' % token", and see if you get a speed 
JS> increase.  (You're saving at least a function call each time, and 
JS> possibly a dictionary construction as well, depending on how vars() 
JS> operates.)  I wouldn't expect a large increase but it should help.
It really accelerates. Especially form I used:
   token = 'tx<nu<nu<nu<nu<' + token + '\n'
(at least ActiveState Pyton2.2)

And several notes to OP:

take a look at several tools:

1. Psyco.
2. mxTexttools

Both of them are rumored to be real accelerators. And even Python2.3
might be of help as they say that every version of Python is faster.

-- 
Best regards,
 anton                            mailto:antonmuhin@rambler.ru