dpitch40 dpitch40 - 21 days ago 6
Python Question

Python 2.7: Time taken by inspect.stack

I was just profiling my Python program to see why it seemed to be rather slow. I discovered that the majority of its running time was spent in the inspect.stack method (for outputting debug messages with modules and line numbers), at 0.005 seconds per call. This seems rather high; is inspect.stack really this slow, or could something be wrong with my program?

Answer

inspect.stack() does two things:

  • collect the stack by asking the interpreter for the stack frame from the caller (sys._getframe(1)) then following all the .f_back references. This is cheap.

  • per frame, collect the filename, linenumber, and source file context (the source file line plus some extra lines around it if requested). The latter requires reading the source file for each stack frame. This is the expensive step.

To switch off the file context loading, set the context parameter to 0:

inspect.stack(0)

Even with context set to 0, you still incur some filesystem access per frame as the filename is determined and verified to exist for each frame.