| title | description | ms.date | ms.topic | author | ms.author | manager | ms.subservice |
|---|---|---|---|---|---|---|---|
Use profiler to measure Python code performance |
Use the Visual Studio profiler for debugging and check the performance of Python code when using CPython-based interpreters. |
09/01/2023 |
how-to |
cwebster-99 |
cowebster |
jmartens |
python |
You can profile a Python application when using CPython-based interpreters. (See Profiling tools with comprehensive reporting for the availability of this feature for different versions of Visual Studio.)
Profiling is started through the Debug > Launch Python Profiling menu command, which opens a configuration dialog:
:::image type="content" source="media/profiling-start.png" alt-text="Screenshot of Profiling configuration dialog.":::
When you select OK, the profiler runs and opens a performance report through which you can explore how time is spent in the application: ::: moniker range="<=vs-2019" :::image type="content" source="media/profiling-results.png" alt-text="Screenshot of Profiling performance report."::: ::: moniker-end ::: moniker range=">=vs-2022" :::image type="content" source="media/profiling-results-vs-22.png" alt-text="Screenshot of Profiling performance report."::: ::: moniker-end
Note
When you profile a Python application, Visual Studio collects data for the lifetime of the process. We do want to hear your feedback on future capabilities. Use the Product feedback button at the bottom of this page.
Because IronPython isn't a CPython-based interpreter, the profiling feature doesn't work.
Instead, use the Visual Studio .NET profiler by launching ipy.exe directly as the target application, using the appropriate arguments to launch your startup script. Include -X:Debug on the command line to ensure that all of your Python code can be debugged and profiled. This argument generates a performance report including time spent in the IronPython runtime and your code. Your code is identified using mangled names.
Alternately, IronPython has some of its own built-in profiling, but there's currently no good visualizer for it. See An IronPython Profiler (MSDN blogs) for what's available.