Python探查器与对象的使用

day*_*mer 5 python profiler

我有一个关于profiler用法的具体问题.我是python编程的新手我试图分析一个我想要作为类方法调用的函数,就像这样

import profile

class Class:
    def doSomething():
        do here ..

    def callMethod():
        self.doSomething()
Run Code Online (Sandbox Code Playgroud)

而不是我想要使用

    profile.run(self.doSomething())
Run Code Online (Sandbox Code Playgroud)

profile.run预计该字符串里面,我得到错误

TypeError: exec: arg 1 must be a string, file, or code object
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

谢谢

day*_*mer 11

固定!!!

我使用cProfile模块而不是profile,根据python文档的开销要小得多

参考:http://docs.python.org/library/profile.html#introduction-to-the-profilers

使用cProfiler,实际上可以使用runctx模块传递本地和全局参数,因此对于同样的问题,我做了以下操作:

import cProfile
cProfile.runctx('self.doSomething()',globals(),locals())
Run Code Online (Sandbox Code Playgroud)

它工作:)

另外,如果你有更多的参数通过你可以喜欢

import cProfile
cProfile.runctx('self.doSomething(x,y,z)',globals(),locals())
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助