Strange interaction between timeit and recursion
Diez B. Roggisch
deets at nospam.web.de
Sun May 3 11:59:41 EDT 2009
Steven D'Aprano schrieb:
> I'm seeing a strange interaction between timeit and recursion.
>
>
>>>> sys.getrecursionlimit()
> 1000
>>>> from timeit import Timer
>>>> setup = """def test(n=1):
> ... if n < 999: return test(n+1)
> ... return None
> ... """
>>>> exec setup
>>>> test() is None
> True
>>>> Timer('test()', setup).repeat(number=1)
> 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
More information about the Python-list
mailing list