getrecursiondepth
Manlio Perillo
NOmanlio_perilloSPAM at libero.it
Fri Oct 1 07:40:42 EDT 2004
On Thu, 30 Sep 2004 17:59:04 GMT, Manlio Perillo
<NOmanlio_perilloSPAM at libero.it> wrote:
>On Tue, 28 Sep 2004 16:26:16 GMT, Andrew Dalke <adalke at mindspring.com>
>wrote:
>
> [...]
>
>And here is an example:
>
>>>> def foo(n = 0):
> if firstlevel_call():
> pystate.rescale()
>
> #print 'n = %s, recursion depth = %s' % (n,
>pystate.getrecursiondepth())
> if n == 5: return
> foo(n + 1)
>
>
>
>>>> foo()
>n = 0, recursion depth = 0
>n = 1, recursion depth = 1
>n = 2, recursion depth = 2
>n = 3, recursion depth = 3
>n = 4, recursion depth = 4
>n = 5, recursion depth = 5
>
>
Actually this can be done without using getrecursiondepth:
>>> def foo2(n = 0):
if pystate.firstlevel_call():
foo2.recursiondepth = 0
else:
foo2.recursiondepth += 1
#print 'n = %s, recursion depth = %s' % (n,
foo2.recursiondepth)
if n == 5: return
foo2(n + 1)
I have timed the two versions
(Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)]
on win32)
timeit.py -s "from time_pystate import foo" "foo()"
10000 loops, best of 3: 21.5 usec per loop
timeit.py -s "from time_pystate import foo2" "foo2()"
10000 loops, best of 3: 26.8 usec per loop
Regards Manlio Perillo
More information about the Python-list
mailing list