Python inspect.stack is slow

后端 未结 2 1263
半阙折子戏
半阙折子戏 2021-01-12 06:56

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()

相关标签:
2条回答
  • 2021-01-12 07:13

    inspect.stack(0) can be faster than inspect.stack(). Even so, it is fastest to avoid calling it altogether, and perhaps use a pattern such as this instead:

    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 will then end.

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

    As an alternative, inspect.getouterframes(inspect.currentframe()) can be looped over, but this is expected to be slower than the above approach.

    0 讨论(0)
  • 2021-01-12 07:24

    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.

    0 讨论(0)
提交回复
热议问题