在python中分析自我和参数?

Dan*_*Dan 3 python profiling

如何在python中配置涉及self和arguments的调用?

def performProfile(self):
    import cProfile
    self.profileCommand(1000000)

def profileCommand(self, a):
    for i in a:
        pass
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我将如何描述对profileCommand的调用?我想我需要使用runctx作为参数,但我该如何处理self?(代码实际上涉及UI,因此很难单独调用配置文件).

Anu*_*yal 11

你需要传递locals/globals dict并传递你通常会输入的第一个参数,例如

cProfile.runctx("self.profileCommand(100)", globals(),locals())
Run Code Online (Sandbox Code Playgroud)

使用这样的东西

class A(object):
    def performProfile(self):
        import cProfile
        cProfile.runctx("self.profileCommand(100)", globals(),locals())

    def profileCommand(self, a):
        for i in xrange(a):
            pass
        print "end."

A().performProfile()
Run Code Online (Sandbox Code Playgroud)

并且不要在配置文件中调用整个UI,分析特定的功能或计算