|
From: Williams, T. W. <TIM...@sa...> - 2001-11-13 20:32:06
|
Good grief! I concur too. Let me explain my point a bit clearer here: o I assumed he was constrained to solving the problem recursively. o I made that assumption because a frequently used method of teaching recursion includes using a simple problemset, ie. fibonacci. o This is mainly because once situations are complex enough to need recursion, if a person doesn't already know it then they are stuck, for instance learning how to implement a B+Tree and traversing it is a good time to *already* know recursion. o If all he wanted was a fast way to compute the fib sequence, he probably should have asked "how can I make this funcion more efficient" rather than "how can I make this recursive function more efficient" then I would have been less likely to make assumptions about his constraints. o He could clear it up by letting us know what his constraints were or were not. I'm not arguing for a recursive solution in this case if he's doing it for any *real* purpose, although I must say I didn't see any significant performance difference for any relatively small n. Just out of curiousity how did you initially learn recursion? I'm guessing it wasn't implementing a tree structure for the first time, right? Probably the fib sequence, I think it's most ever intro to CS book out there. --tim > -----Original Message----- > From: Ioannis Vranos [mailto:no...@ya...] > Sent: Tuesday, November 13, 2001 2:52 PM > To: Dev-C++ > Subject: RE: [Dev-C++] Efficiency > > > I astrongly agree with St0fF here. Recursion has time cost due to > continuous function calls and space cost because of the storage of the > function calls in the stack (or in the automatic storage if > you prefer). > This applies to fibonacci too. > > > Ioannis > > * Ioannis Vranos > * Programming pages: http://www.noicys.f2s.com > * Alternative URL: http://run.to/noicys > > > > -----Original Message----- > > From: dev...@li... > > [mailto:dev...@li...] On Behalf > > Of St0fF 64 > > Sent: Tuesday, November 13, 2001 5:48 PM > > To: DevCPP; Williams, Timothy W. > > Subject: Re: [Dev-C++] Efficiency > > > > > > Hi! > > As I said, iteration speeds it all up because no unneccessary > > Stacking, > > meaning: push return address, recursive recall, push return > > address, recursive recall, ... , recursive recall, return > > solution, pop return address, return solution, ... , pop > > return address, return solution, finished. This is the > > negative effect of recursion. In the iterative version we do > > counting instead of accessing the stack. The dealing with > > return addresses and the stack is not used in iteration. > > Thats why it is faster. St0fF. > > > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |