On Mon, 2009-05-04 at 00:34 +1000, Steven D'Aprano wrote:
Okay, this toy program isn't exactly a mission-critical application, but it does demonstrate a genuine performance penalty when using recursion. In this case, two recursive calls (one of which is a tail-call) takes nearly 60% more time than a single recursive call in a while loop.
This doesn't demonstrate where the issue is. Is it function calls? The tail recursion call makes (eyeball count) twice as many python function calls (not calls into generators) per call to move(2)? Or perhaps something else? Sure, the TCO one is slower, but is the handing of result from generator to generator really the issue? If it is rather something else, it may be that the nested yielding while costing, is not costing disproportionately - and is still a fraction of the cost. python -m timeit 'from foo import do_move, do_move2' 'do_move()' 65535 10 loops, best of 3: 164 msec per loop python -m timeit 'from foo import do_move, do_move2' 'do_move2()' 32768 10 loops, best of 3: 108 msec per loop Half as many function calls, 2/3rds the time. *Much* better measurement than these sketches is needed to say where the issue is. -Rob