Python 分析器与对象的使用
我有一个关于探查器使用的具体问题。我是 python 编程新手 我正在尝试分析一个我想作为类方法调用的函数,
import profile
class Class:
def doSomething():
do here ..
def callMethod():
self.doSomething()
我想使用类似这样的函数而不是这个
profile.run(self.doSomething())
,但是 profile.run
需要其中的字符串,并且我收到错误
TypeError: exec: arg 1 must be a string, file, or code object
有人可以吗请帮忙?
谢谢
I have a specific question regarding the usage of profiler. I am new to python programming
I am trying to profile a function which I want to invoke as a class method, something like this
import profile
class Class:
def doSomething():
do here ..
def callMethod():
self.doSomething()
instead of this I want to use
profile.run(self.doSomething())
but the profile.run
expects the string inside it and I get error
TypeError: exec: arg 1 must be a string, file, or code object
Can somebody please help?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
固定的!!!
我使用的不是 profile,而是 cProfile 模块,根据 python 文档,该模块的开销要小得多
参考:http://docs.python.org/library/profile.html#introduction-to-the-profilers
使用 cProfiler,实际上可以使用 runctx 模块传递本地和全局参数
所以对于同样的问题,我做了以下操作:
并且它有效:)
另外,如果您有更多参数要传递,您可以喜欢
感谢所有帮助
Fixed!!!
Instead of profile, I used cProfile module that as per the python docs has much lesser overhead
Ref : http://docs.python.org/library/profile.html#introduction-to-the-profilers
with cProfiler, one can actually pass the local and global params using the runctx module
so for the same problem, I did the following:
and it worked :)
also, if you have more params to pass you can like
Thanks for all help
您需要修复各种不精确之处(缺少
self
,表示在看不到classmethod
时您正在使用类方法,无法从object
继承, ...) 然后通过给它一个想要的字符串来让profile
满意 - 并且实例的名称必须全局可见,以便profile
可以实际使用那个字符串。例如:You need to fix various imprecisions (missing
self
, saying you're using class methods when there's noclassmethod
in sight, failing to inherit fromobject
, ...) then makeprofile
happy by giving it a string as it wants -- and the name of the instance must be made globally visible so thatprofile
can actually use that string. For example: