Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does pypy handle recursions?

Tags:

python

pypy

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.

like image 592
Lostsoul Avatar asked Sep 24 '11 18:09

Lostsoul


1 Answers

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
like image 60
zeekay Avatar answered Sep 22 '22 14:09

zeekay