pypy 如何处理递归?

发布于 2024-12-06 17:23:54 字数 394 浏览 0 评论 0原文

我有一个用 python 编写的脚本,它工作得很好,但我很好奇是否可以加快它的速度。它基本上是递归脚本。

如果我在普通的 python 2.7 中运行它,大约需要 30 秒。当我使用 pypy 运行相同的操作时,出现以下错误:

RuntimeError: maximum recursion depth exceeded

我不确定 pypy 做了什么不同的事情,因为我没有修改脚本。

谁能帮助我了解发生了什么事?

更新: 好吧,我想通了。增加限制有所帮助,但我认为我运行了错误的文件。我在 bin 目录下找到了一个名为 py.py 的文件并正在使用它。我不确定该文件的作用,但它比普通的 python 慢。我不得不搜索并发现“pypy-c”现在似乎可以工作。

I have a script I wrote in python and it works fine but I was curious to see if I could speed it up. It basically is recursive script.

If I run it within normal python 2.7, it takes about 30 seconds. When I run the same thing using pypy than I get the following error:

RuntimeError: maximum recursion depth exceeded

I'm not sure what pypy is doing differently because I'm not modifying the script.

Can anyone help me understand what is going on?

Update:
ok I figured it out. Increasing the limit helped but I think I was running the wrong file. I found a file under the bin directory called py.py and was using that. I'm not sure what the file does but its slower than normal python. I had to search and find 'pypy-c' seems to work now.

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

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

发布评论

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

评论(1

洛阳烟雨空心柳 2024-12-13 17:23:54

正如您在更新中所建议的,您的问题是您正在使用 py.py (用于在 CPython 之上运行 PyPy 的解释器)。 PyPy 通常比 CPython 具有更高的递归限制。您可以使用sys.setrecursionlimit()来增加递归限制,sys.getrecursionlimit()不提供实际的递归限制。

PyPy 1.6.0:

>>>> sys.getrecursionlimit()
100

>>>> def infinite(level=0):
....     print level
....     return infinite(level+1)
.... 

>>> infinite()
<snip>
1010
Traceback (most recent call last):
  File "<console>", line 2, in infinite
RuntimeError: maximum recursion depth exceeded

>>> sys.setrecursionlimit(sys.maxint)

>>> infinite()
<snip>
9769
zsh: segmentation fault  pypy

As you suggest in your update your problem was that you were using py.py (which is for running PyPy's interpreter on top of CPython). PyPy has a higher recursion limit than CPython normally. You can use sys.setrecursionlimit() to increase the recursion limit, sys.getrecursionlimit() does not provide the actual recursion limit.

PyPy 1.6.0:

>>>> sys.getrecursionlimit()
100

>>>> def infinite(level=0):
....     print level
....     return infinite(level+1)
.... 

>>> infinite()
<snip>
1010
Traceback (most recent call last):
  File "<console>", line 2, in infinite
RuntimeError: maximum recursion depth exceeded

>>> sys.setrecursionlimit(sys.maxint)

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