pypy 如何处理递归?
我有一个用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您在更新中所建议的,您的问题是您正在使用 py.py (用于在 CPython 之上运行 PyPy 的解释器)。 PyPy 通常比 CPython 具有更高的递归限制。您可以使用
sys.setrecursionlimit()
来增加递归限制,sys.getrecursionlimit()
不提供实际的递归限制。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.