Skip to content

[PyCharm] Use Profile to Analyze Program Performance

In the developing process, apart from carefully debug, we often worry about the execution efficiency of the program.

Fortunately, in PyCharm, an easy-to-use IDE, we can simply analyze the program's performance through the Profile function.

If you are interested in PyCharm, you can refer: PyCharm] Installation Tutorial, A famous Python IDE


Simple Method

Before we start to introduce the Profile, I want to record another simpler method.

For example, today we develop a program to solve a problem, we can calculate the time from when the program is executed to when the problem is solved.

In Linux, we can use time command to display runtime.

time python3 your_program.py


Or you can use the built-in time module in Python.

import time

start_time = time.time()

# Your Code ...


print('Cost Time: {}s'.format(time.time()-start_time))





Profile

Using the Profile in PyCharm is actually very simple. There is an icon like this in the upper right corner of PyCharm:

This is the execution button of the Profile. After pressing it, the program will start to execute. If there is no window other than the runtime Log, please press the button in the diagram:

Generally speaking, a form like the following is printed:

There are 3 fields, namely:

  • Call Count
  • Time (ms)
  • Own Time (ms)

Among them, Call Count is well understood, which is the number of times the area has been called. In addition, I don’t know the difference between Time and Own Time very clearly. However, according to the opinions of everyone on the Internet, they tend to optimize Own Time first.

In addition, you can also see the Call Graph:

For example, my program should focus on optimizing the main program main.py.

Leave a Reply