Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python cProfile - most of time spent in method 'enable' of '_lsprof.Profiler'

I am trying to perform some high-level profiling of a rather complex Python program. However, when using cProfile, almost all time is measured in:

{method 'enable' of '_lsProf.Profiler' objects}

It happens if I profile the whole program python -m cProfile ... as well as when I perform profiling within the Python code (with profile.enable()/.disable()/.create_stats...)

Any pointers to what I could be doing wrong?

like image 611
Simon Heinzle Avatar asked Aug 18 '17 19:08

Simon Heinzle


People also ask

How do you profile a Python code using cProfile?

The syntax is cProfile. run(statement, filename=None, sort=-1) . You can pass python code or a function name that you want to profile as a string to the statement argument. If you want to save the output in a file, it can be passed to the filename argument.

Which tool will analyze the data collected by the Python profiler?

Analysis of the profiler data is done using the Stats class. This class constructor creates an instance of a “statistics object” from a filename (or list of filenames) or from a Profile instance.

Which Python module can profile Python code in a sense to analyze its performance?

In Python's standard library, we have the timeit module that allows us to do some simple profiling. The output of timeit is to find the best performance among multiple runs (default to be 5).


1 Answers

It's because somewhere in your code you have something like

import cProfile
pr = cProfile.Profile()
pr.enable()

That's for manually saving the results or printing them, if you are calling the profiler like you said with python -m cProfile -o program.prof my_program.py you don't need to use cProfile inside the program.

like image 91
Computer's Guy Avatar answered Sep 16 '22 23:09

Computer's Guy