【问题标题】:cProfile inside a nested function嵌套函数内的 cProfile
【发布时间】:2018-06-25 17:59:36
【问题描述】:

我正在尝试使用 cProfile.run 分析嵌套函数。我知道cProfile 可能与我调用它的范围不同,但我不太确定实现这一目标的惯用方法是什么。这是一个 MVCE:

def foo():
    def bar():
        # do something here
        return 1
    cProfile.run('bar()')

给出错误:

NameError: name 'bar' is not defined

【问题讨论】:

    标签: python cprofile


    【解决方案1】:

    使用cProfile.runctx:

    def foo():
        def bar():
            # do something here
            return 1
        cProfile.runctx('bar()', None, locals=locals())
    

    【讨论】:

      【解决方案2】:

      使用 cProfile.run

      def foo():
          def bar():
              # do something here
              return 1
          cProfile.run(bar.__code__)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多