Eclipse 中的 Python 分析

发布于 2024-07-14 20:20:39 字数 364 浏览 6 评论 0原文

这个问题是半基于这个问题的:

How can you profile a python script ?

我认为这对于我的一些程序来说是个好主意。 尽管可以按照上述答案中的说明从批处理文件进行分析,但我认为在 Eclipse 中提供此选项会更好。 同时,将我的整个程序变成一个函数并对其进行分析意味着我必须更改源代码?

如何配置 eclipse 以便能够在现有程序上运行 profile 命令?

欢迎任何提示或建议!

This questions is semi-based of this one here:

How can you profile a python script?

I thought that this would be a great idea to run on some of my programs. Although profiling from a batch file as explained in the aforementioned answer is possible, I think it would be even better to have this option in Eclipse. At the same time, making my entire program a function and profiling it would mean I have to alter the source code?

How can I configure eclipse such that I have the ability to run the profile command on my existing programs?

Any tips or suggestions are welcomed!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花易冷人易散 2024-07-21 20:20:39

如果您遵循常见的 python 习惯用法来使所有代码(甚至“现有程序”)可作为模块导入,那么您可以完全按照您所描述的方式进行操作,而无需任何额外的麻烦。

这是我正在谈论的特定习惯用法,它使您的程序流程“颠倒”,因为一旦您的所有 __name__ == '__main__' 将被放置在文件的底部>def 已完成:

# program.py file

def foo():
    """ analogous to a main().  do something here """
    pass

# ... fill in rest of function def's here ...

# here is where the code execution and control flow will
# actually originate for your code, when program.py is
# invoked as a program.  a very common Pythonism...
if __name__ == '__main__':
    foo()

根据我的经验,改造任何现有脚本以遵循此形式非常容易,最多可能几分钟。

由于让您对模块进行编程还有其他好处,因此您会发现大多数 python 脚本实际上都是这样做的。 这样做的一个好处是:您编写的任何 python 都可以以模块形式使用,包括 foo()cProfile-ing。

if you follow the common python idiom to make all your code, even the "existing programs", importable as modules, you could do exactly what you describe, without any additional hassle.

here is the specific idiom I am talking about, which turns your program's flow "upside-down" since the __name__ == '__main__' will be placed at the bottom of the file, once all your defs are done:

# program.py file

def foo():
    """ analogous to a main().  do something here """
    pass

# ... fill in rest of function def's here ...

# here is where the code execution and control flow will
# actually originate for your code, when program.py is
# invoked as a program.  a very common Pythonism...
if __name__ == '__main__':
    foo()

In my experience, it is quite easy to retrofit any existing scripts you have to follow this form, probably a couple minutes at most.

Since there are other benefits to having you program also a module, you'll find most python scripts out there actually do it this way. One benefit of doing it this way: anything python you write is potentially useable in module form, including cProfile-ing of your foo().

挽梦忆笙歌 2024-07-21 20:20:39

您始终可以创建单独的模块,只对其他模块中的特定内容进行分析。 您可以将此类模块组织在单独的包中。 这样您就不会更改现有的代码。

You can always make separate modules that do just profiling specific stuff in your other modules. You can organize modules like these in a separate package. That way you don't change your existing code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文