[Tutor] Slow program

Magnus Lycka magnus@thinkware.se
Wed Dec 11 15:40:02 2002


At 10:46 2002-12-11 -0800, Danny Yoo wrote:
>Here's one way to do that code in a linear scan: ...

There were a few typos here, but I compared this with the
original program, and it makes quite a difference. If you use
the function below which is based on Danny's code and the
original function, you can profile them with the code below.

In this case it's obvious that you will benefit from going
from a loop inside a loop, to one single loop, but as program
get longer, it usually gets very difficult to spot manually
where our bottle-necks are.

There is no reason to think about any measurements if the app
is already fast enough, but if there are performance problems,
it's very often really helpful to use the profiler.

So, getting used to using the profiler is a good thing. In addition
to this trivial usage, we can make a program run save data in a
statistics file, that we analyze with the pstats module.

The disadvantage with the profiler is that it slow down the code
a lot. There is a new profiler written in C called hotshot. It's
included in Python (try "import hotshot") and it's much faster,
but it's not quite ready, and it's not documented. See
http://web.pydoc.org/2.2/hotshot.html and
starship.python.net/crew/fdrake/talks/ IPC10-HotShot-2002-Feb-06.ppt


def calc2(b):
     dic = {}
     for x in range(len(b)-1):
         pair = b[x], b[x+1]
         dic[pair] = dic.get(pair, 0) + 1
     ma=0
     for x in range(len(dic)):
         if dic.values()[x] > ma:
             ma = dic.values()[x]
     for x in range(len(dic)):
         if dic.values()[x] == ma:
             print "%s -> %d" % (" ".join(dic.keys()[x]),dic.values()[x])

b = "some suitably big string..."

import profile
print "original"
profile.run('calc(b)')
print "Danny Yoo"
profile.run('calc2(b)')



-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se