[Tutor] script too slow
Paul Tremblay
phthenry@earthlink.net
Sat Mar 1 04:12:01 2003
Thanks everyone for your help. I re-wrote the script using the
suggestions of several people.
I put a snippet of the code at the bottom.
The old script took 1 minutes and 30 seconds to run. The new one takes
57 seconds. That is a dramatic improvement.
Remember my fear was that the Python version would run 300 percent
slower than the perl one. However, this is how things are shaping up:
tokenze:
perl => 7 seconds
python => 22 seconds
(Wow! perl really is fast here.)
processing tokens:
perl => 27 seconds
Python => 36
(Pyton might be faster in other areas later on.)
So Python is 40 percent slower in the second phase.
I'm not saying this is in any way a benchmark. However, at this point I
am getting the script to run 40 percent slower. I can live with that.
That means a 2 minutes in perl *might* take less than 3 in Python.
Before I was looking at 6 minutes
*********************************************************
# rewritten script uses dictionaries as 'case' statements
# This replaces the multiple if elif statements
# The resulting code is amazing simple and clean
def initiate_dict(self):
self.dict_token =
{
'*' : ('*', self.default_func),
':' : (':', self.default_func),
'{' : ('{', self.ob_func),
'}' : ('}', self.cb_func),
# ect hundreds of lines of dictionary entries
}
def process_cw(self, token):
# some minor preliminary stuff for small exceptions
token, action = self.dict_token.get(token, (None, None))
if action:
to_print = action(token, num)
return to_print
def process_tokens(self):
# open file and read one token at a time...
...
if token[0:1] == "\\":
line = self.process_cw(token)
if line != None:
write_obj.write(line)
else:
line = 'tx<nu<nu<nu<nu<%s\n' % token
write_obj.write(line)
# can I do:
# write_obj.write('tx<nu<nu<nu<nu%s\n' % token) ?
# probably--will have to try
# That's it! I was able to take out maybe 50 lines of code.
# The result is code that runs 200 to 300 percent faster
**********************
--
************************
*Paul Tremblay *
*phthenry@earthlink.net*
************************