CPU usage increasing over time
Question: I have a python program that is running for many days. Memory usage does not
increase by very much, however the program becomes slower and slower. Is there
a tool or utility that will list all function calls and how long they take to
complete? Something like [guppy/heapy](http://guppy-pe.sourceforge.net/) but
for CPU usage.


Answer: **Edit2**

I just saw your actual question is answered as in '[How can you profile a
Python script?](http://stackoverflow.com/questions/582336/how-can-you-profile-
a-python-script)'

* * *

Sure, use profile.py.

    
    
    import profile
    
    def myfunction():
        bla bla bla
    
    profile.run('myfunction()')
    

see also [profilers](http://docs.python.org/library/profile.html) and [tips on
performance](http://wiki.python.org/moin/PythonSpeed/PerformanceTips).

**Edit:** Above example is for one function. You can profile and run your
script from the commandline with cProfile with:

    
    
    python -m cProfile myscript.py
    

Your program/script could look also like the following for profiling always
when run:

    
    
    def myfunction():
        for i in range(100):
            print(i)
    
    def myotherfunction():
        for i in range(200):
            print(i)
    
    def main():
        """ main program to run over several days """
        for _ in range(3):
            myfunction()
    
        myotherfunction()
    
    if __name__ == '__main__':
        profile.run('main()') # will execute your program
                              # and show profiling results afterwards
    

