PyCharm 2023.1 Help

Optimize your code using profilers

PyCharm allows running the current run/debug configuration while attaching a Python profiler to it. Note that the Diagrams plugin that is bundled with PyCharm should be enabled.

If you have a yappi profiler installed on your interpreter, PyCharm starts the profiling session with it by default, otherwise it uses the standard cProfile profiler.

Besides these two tracing profilers, PyCharm supports also sampling (statistical) profiler vmprof, which should be installed on the selected Python interpreter. If you are Windows 64 bit user, you have to install Python 32 bit, to make vmprof work. Install 32-bit Python as described on the page Python Releases for Windows.

A profiler runs in the following order: vmprof, yappi, cProfile.

Start the profiling session

  1. Click profiler on the main toolbar and select Profile <current run.debug configuration name> or select the same command from Run in the main menu.

  2. Ensure that the profiler has started in the dedicated tab of the Run tool window.

    Results

    Item

    Description

    Profiling results

    Saves the profiling results in the <project name>.pstat file for cProfile profiler and the <project name>.prof file for vmprof.

    Stop profiling

    Stops the profiler.

    Close

    Closes the profiler tab.

Work with the profiling results

  1. On the toolbar of the profiler tab in the Run tool window, click Capture.

  2. Inspect the profiling results:

    The snapshot is saved to the default location under %LOCALAPPDATAPATH%\JetBrains\<product><version>/snapshots on Windows and ~/Library/Caches/JetBrains/<product><version>/snapshots on macOS. For CProfiler, it is saved as <project name>.pstat file. The file opens in the <project name>.pstat tab in the editor, which consists of two tabs: Statistics and Call Graph:

    Results

    Item

    Description

    Name

    The name of the function.

    Call Count

    Number of calls of the chosen function.

    Time

    Execution time of the chosen function plus all time taken by functions called by this function. The percentage of time spent in this call related to time spent in all calls in the parentheses.

    Own Time

    Own execution time of the chosen function. The percentage of own time spent in this call related to overall time spent in this call in the parentheses.

    Tip: To search for a particular file in the Name column of the Statistics table, click any cell and start typing the target name. The Search field appears in the header of the column so that you can edit the search criteria.

    The snapshot is saved to the default location under %LOCALAPPDATAPATH%\JetBrains\<product><version>/snapshots on Windows and ~/Library/Caches/JetBrains/<product><version>/snapshots on macOS. For vmprof, it is saved as <project name>.prof file.

    The file open in the <project name>.pstat tab in the editor, which consists of three tabs: Statistics, Call Graph, and Call Tree

    Call tree

    Tip: Jump to the source code of any file and preview the Profile lines profiling results in the gutter. Lines consuming more processor time are marked yellow and red.

    Profile lines

Jump to the source code

  • To navigate to the source code of a certain function, right-click the corresponding entry on the Statistics tab, and choose Navigate to Source from the context menu:

    Jump to source

    The source code of the function opens in the editor.

View the Call Graph

  1. To navigate to the call graph of a certain function, right-click the corresponding entry on the Statistics tab, and choose Show on Call Graph from the context menu.

    The Call Graph tab opens with the selected function:

    Call graph

    Note the color codes on the Call Graph. The functions marked red consume more time; the fastest functions are green.

  2. Use the toolbar buttons to work with the graph:

    Item

    Description

    Zoom In

    Click this icon to increase the scale of the diagram. Alternatively, press NumPad-NumPad-+.

    Zoom Out

    Click this icon to decrease the scale of the diagram. Alternatively, press NumPad-NumPad--.

    Actual Size

    Click this icon to restore the actual size of the diagram.

    Fit Content

    Click this icon to make the contents fit into the current diagram size.

    Apply Current Layout

    Click this icon to apply the current layout, selected from the Layout node of the diagram context menu.

    Route Edges

    Click this icon to scale the diagram to the edges of the window.

    Copy Diagram to Clipboard

    Click this icon to copy the diagram into the clipboard.

    Copy Selection to Clipboard

    Click this icon and then select any diagram area with the mouse cursor: the selected area will be copied into the clipboard.

    Export Diagram

    Click this icon to export a diagram using various formats, which are compatible with third-party tools, to a file, to an image, or open the diagram in the browser.

    Export Diagram

    You can select and configure the following options:

    • Open in Web Editor: click this button to select a third-party editor to which you can export the diagram.

    • Copy Diagram to Clipboard: click this button to save the diagram to a clipboard using one of the available formats.

    • Save diagram: click this button to save the current diagram as a file. You can use one of the available formats from the list.

    • Save image: click this button to save the current diagram in the PNG format.

    • Print: click this button to print the diagram.

    Refresh Data Model

    Click this button to refresh the whole diagram.

    Open Diagram Settings

    Click this button to open the Diagram settings where you can configure additional options.

Review the existing cProfile snapshots

  1. From the main menu, choose Tools | Open CProfile snapshot.

  2. In the Select PStat file dialog, choose the desired file with the extension pstat.

    The profiling results open in the <project name>.pstat tab in the editor.

Last modified: 21 June 2023