Python inspect.stack is slow

I just profiled my Python program to see why it was rather slow. I found that most of his time was spent on the inspect.stack() method (for outputting debugging messages with modules and line numbers) for 0.005 seconds per call. It seems pretty tall; inspect.stack really slow or is something wrong with my program?

+4
source share
2 answers

inspect.stack() does two things:

  • assemble the stack by asking the interpreter for the stack frame from the caller ( sys._getframe(1) ), and then following all the .f_back links. It is cheap.

  • per frame, collect the file name, linenumber and the original file context (line of the source file plus some additional lines around it, if necessary). The latter requires reading the source file for each frame of the stack. This is an expensive step.

To disable file context loading, set the context parameter to 0 :

 inspect.stack(0) 

Even if the context is set to 0, you still have access to the file system for each frame, since the file name is determined and checked for presence for each frame.

+10
source

inspect.stack(0) can be faster than inspect.stack() . However, this is the fastest way to not invoke it at all, and perhaps use a template instead, for example:

 frame = inspect.currentframe() while frame: if has_what_i_want(frame): # customize return what_i_want(frame) # customize frame = frame.f_back 

Note that the last frame.f_back is None , and the loop ends.

sys._getframe(1) will obviously not be used because it is an internal method.

Alternatively, inspect.getouterframes(inspect.currentframe()) can be looped, but it is expected to be slower than the approach described above.

+6
source

Source: https://habr.com/ru/post/1489114/


All Articles