Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm running a relatively complex python program and in it there is a montecarlo simulation which takes up most of the time. I would like to find out what part of it uses the most resources so I can potentially make it faster.

I'm using PyCharm Professional edition and tried to use the profiler, but the result is only a large list of irrelevant functions that I've never heard of.

Questions: Is there a good profiler I can use that delivers meaningful results so I can see which function or keyword uses the most resources in my montecarlo simulation?

like image 622
Nickpick Avatar asked Oct 03 '15 19:10

Nickpick


People also ask

How do I profile a Python script in PyCharm?

Start the profiling sessionon the main toolbar and select Profile <current run. debug configuration name> or select the same command from Run in the main menu. Ensure that the profiler has started in the dedicated tab of the Run tool window. Saves the profiling results in the <project name>.

Is PyCharm an IDE for Python?

PyCharm is a dedicated Python Integrated Development Environment (IDE) providing a wide range of essential tools for Python developers, tightly integrated to create a convenient environment for productive Python, web, and data science development.

What is a Python profiler?

Introduction to the profilers cProfile and profile provide deterministic profiling of Python programs. A profile is a set of statistics that describes how often and for how long various parts of the program executed. These statistics can be formatted into reports via the pstats module.


1 Answers

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 cProfile  pr = cProfile.Profile() pr.enable() your_function_call() pr.disable() # after your program ends pr.print_stats(sort="calls") 
like image 50
shafeen Avatar answered Sep 19 '22 13:09

shafeen