Return value while using cProfile

后端 未结 5 545
难免孤独
难免孤独 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条回答
  •  Happy的楠姐
    2020-12-13 07:37

    I think @detly the .runcall() is basically the best answer, but for completeness, I just wanted to take @ThomasH 's answer to be function independent:

    def wrapper(b, f, *myargs, **mykwargs):
        try:
            b.append(f(*myargs, **mykwargs))
        except TypeError:
            print 'bad args passed to func.'
    
    # Example run
    def func(a, n):
        return n*a + 1
    
    b = []
    cProfile.runctx("wrapper(b, func, 3, n=1)", globals(), locals())
    a = b[0]
    print 'a, ', a
    

提交回复
热议问题