rook
rook

Reputation: 67019

CPU usage increasing over time

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 but for CPU usage.

Upvotes: 1

Views: 1672

Answers (2)

Remi
Remi

Reputation: 21165

Edit2

I just saw your actual question is answered as in 'How can you profile a Python script?'


Sure, use profile.py.

import profile

def myfunction():
    bla bla bla

profile.run('myfunction()')

see also profilers and tips on performance.

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

Upvotes: 2

krisdigitx
krisdigitx

Reputation: 7126

get the pid of the process

ps -ef | grep "processname" | awk '{print $2}

and then see which calls in the program are taking more time.

strace -c -p "pid"

you can even run the whole script with

strace -c script.file

Upvotes: 0

Related Questions