ipython 分析器

发布于 2025-01-07 13:04:18 字数 797 浏览 0 评论 0原文

我有一些 python 代码可以工作,但不幸的是速度超级慢。 #python 的某人建议我可以通过分析器运行代码,以查看代码花费最多时间的行和函数。

我想要分析的 python 源代码从 STDIN 读取。但由于输入很大,我将输入编译为文件,以便我可以简单地将其重定向到 shell 中的 python 代码。因此,在 shell 中,我发出命令。

cat input | python pythonsource.py 

问题是,当我尝试在 ipython 中运行探查器时,我似乎找不到将输入重定向到 python 代码的方法。在 ipython shell 中,我尝试过,

run -p -l 1.0 pythonsource.py input (didn't work. simply waits at STDIN for input)
run -p -l 1.0 pythonsource.py << input (didn't work)
run -p -l 1.0 cat input | python pythonsource.py (didn't work.)

我不知道该怎么做,我可以使 ipython profiler 命令将输入​​重定向到 STDIN,以便 pythonsource 读取。有人可以告诉我如何解决这个问题吗?或者我完全错了?也许还有其他更干净、更智能的方法来分析 python 代码?

也许我接下来要问的应该是另一个问题的一部分......但我想知道当 ipython 在 ipython 探查器的某些输出中引用“原始调用”时是什么意思?

谢谢。

I have some code in python that works but is unfortunately super slow. Someone at #python suggested that I can run the code through a profiler to see the lines and functions in which the code was taking the most amount of time.

The python source code that I want to profile reads from STDIN. But since the input is large I compiled the input as a file so that I can simply redirect it to the python code at the shell. So at the shell, I issue the command..

cat input | python pythonsource.py 

The problem is, when I try to run the profiler in ipython I can't seem to find a way to redirect the input to the python code. At the ipython shell, I tried,

run -p -l 1.0 pythonsource.py input (didn't work. simply waits at STDIN for input)
run -p -l 1.0 pythonsource.py << input (didn't work)
run -p -l 1.0 cat input | python pythonsource.py (didn't work.)

I'm not sure how to do I can make the ipython profiler command redirect the input to STDIN for the pythonsource to read from. Could someone please tell me how to fix this? Or have I got it totally wrong? Maybe there is some other cleaner, more smarter way of profiling python code?

And maybe what I ask next should be a part of another question..but I was wondering what does ipython mean when it refers to "primitive calls" in some of the output of the ipython profiler?

Thank you.

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

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

发布评论

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

评论(1

指尖上得阳光 2025-01-14 13:04:18

在 shell 命令行上重定向输入的正确方法如下:

cat input | run -p -l 1.0 python pythonsource.py

run -p -l 1.0 python pythonsource.py < input

语法 << string 创建一个“此处文档”,它不会重定向输入。

从 python 提示符(希望在 ipython 中),您可以像这样重定向标准输入:

import sys
save_stdin = sys.stdin
sys.stdin = open('input')
run -p <etc.>
sys.stdin = save_stdin # Restore the real stdin

或者(推荐)您可以重写源代码以使用文件名: 如果您按如下方式调用它,“输入”将在 中sys.argv[1] 你可以打开它并从中读取:

python pythonsource.py input

The right way to redirect input on the shell commandline is as follows:

cat input | run -p -l 1.0 python pythonsource.py

run -p -l 1.0 python pythonsource.py < input

The syntax << string creates a "here document", it does not redirect input.

From the python prompt (and hopefully in ipython), you can redirect standard input like this:

import sys
save_stdin = sys.stdin
sys.stdin = open('input')
run -p <etc.>
sys.stdin = save_stdin # Restore the real stdin

Or (recommended) you could rewrite your source to work with a filename: If you call it as follows, "input" will be in sys.argv[1] and you can open it and read from it:

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