Return value while using cProfile

后端 未结 5 551
难免孤独
难免孤独 2020-12-13 06:59

I\'m trying to profile an instance method, so I\'ve done something like:

import cProfile

class Test():

    def __init__(self):
        pass

    def method         


        
5条回答
  •  悲&欢浪女
    2020-12-13 07:27

    I was struggling with the same problem and used a wrapper function to get over direct return values. Instead of

    cP.runctx("a=foo()", globals(), locales())
    

    I create a wrapper function

    def wrapper(b):
      b.append(foo())
    

    and profile the call to the wrapper function

    b = []
    cP.runctx("wrapper(b)", globals(), locals())
    a = b[0]
    

    extracting the result of foo's computation from the out param (b) afterwards.

提交回复
热议问题