Strange interaction between timeit and recursion

  • Thread starter Steven D'Aprano
  • Start date
S

Steven D'Aprano

I'm seeing a strange interaction between timeit and recursion.

.... if n < 999: return test(n+1)
.... return None
.... """Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/timeit.py", line 188, in repeat
t = self.timeit(number)
File "/usr/lib/python2.5/timeit.py", line 161, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 9, in inner
File "<timeit-src>", line 4, in test
File "<timeit-src>", line 4, in test
File "<timeit-src>", line 4, in test
...
File "<timeit-src>", line 4, in test



I don't understand why my recursive function hits the recursion limit
inside the timeit.Timer when it works outside of it.

Is there any way to see the current recursion depth at a particular
moment?
 
D

Diez B. Roggisch

Steven said:
I'm seeing a strange interaction between timeit and recursion.


... if n < 999: return test(n+1)
... return None
... """
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/timeit.py", line 188, in repeat
t = self.timeit(number)
File "/usr/lib/python2.5/timeit.py", line 161, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 9, in inner
File "<timeit-src>", line 4, in test
File "<timeit-src>", line 4, in test
File "<timeit-src>", line 4, in test
...
File "<timeit-src>", line 4, in test



I don't understand why my recursive function hits the recursion limit
inside the timeit.Timer when it works outside of it.

Is there any way to see the current recursion depth at a particular
moment?

import inspect

def rec(count=100):
if not count:
return
print len(inspect.getouterframes(inspect.currentframe()))
rec(count-1)

rec()

Diez
 
C

Chris Rebert

Recursion is unpythonic.  Do not use it.

That's a tad extreme. I think the accepted practice is merely not to
use recursion gratuitously.

Cheers,
Chris
 
D

Diez B. Roggisch

namekuseijin said:
Recursion is unpythonic. Do not use it.

Since when? Says who?

Lacking tail-recursion, it's not the choice for loops, but whatever
algorithm is recursive can be written as such.

Diez
 
H

Hrvoje Niksic

Steven D'Aprano said:
I don't understand why my recursive function hits the recursion
limit inside the timeit.Timer when it works outside of it.

Probably because your test function is at the very edge of the
recursion limit, and timeit.Timer triggers it because it calls it at a
deeper stack level than is the case with the interactive prompt.
AFAIK the "recursion limit" simply limits the number of nested
function calls.
Is there any way to see the current recursion depth at a particular
moment?

I don't think that's possible from within Python, although maybe it
should be. The current recursion depth is a simple member of the
thread state structure that could be exposed as a low-level function
that begins with an underscore, similar to sys._getframe. The
"inspect" module and various debuggers might put it to good use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top