Profiling a python program with PyCharm (or any other IDE) Profiling a python program with PyCharm (or any other IDE) python python

Profiling a python program with PyCharm (or any other IDE)


Depending on your needs and your python version, maybe you want to use something like hotshot. https://docs.python.org/2/library/hotshot.html

EDIT:

For python 3.4 cProfile is probably one the best options you have available but you will definitely have to filter the results with grep/sed/awk to be able to get the relevant results especially if you use libraries imported where there are a lot of internal calls occurring.

I like sorting by number of calls:python -m cProfile -s 'calls' <your_program>.py

Now the issue in python3 with that method is the number of primitive calls that will show up if cProfile is called externally, so running it internally is probably a better idea:

import cProfilepr = cProfile.Profile()pr.enable()your_function_call()pr.disable()# after your program endspr.print_stats(sort="calls")


Note: As mentioned in the comments, the following applies to the paid version of PyCharm:

If using 3.x (don't know about 2.x), I'll add to shafeen's answer and make it more PyCharm specific as per the original post. This also works better for web applications or larger applications versus simple command line programs where printing the output to stdout might be okay (still better to be able to sort different ways through PyCharm's viewer).

Indeed, do as suggested by instantiating Profile and enabling and disabling as needed. To make that useful though, you'll want to save this to a file.

  • In an outer section of your code, instantiate Profile.
  • In the inner section of your code, do your profiling.
  • Now, call pr.dump_stats('profile.pstat')

You now have a profile file that you would like to examine. Go to Tools|Open CProfile snapshot. Select profile.pstat and now you can view and sort by different headings as desired.

Summary

import cProfile as profile# In outer section of codepr = profile.Profile()pr.disable()# In section you want to profilepr.enable()# code of interestpr.disable()# Back in outer section of codepr.dump_stats('profile.pstat')

Open file in PyCharm's CProfile viewer.