|
From: Per W. <pw...@ia...> - 2003-02-14 20:41:59
|
EMA System email: pw...@ia... Per Westermark web: http://iapetus.neab.net Vargvaegen 174B phone: +46 8 410 17 198 S-906 42 Umeaa Sweden On Fri, 14 Feb 2003, Ioannis Vranos wrote: > -------Original Message------- > > From: Per Westermark > Date: =D0=E1=F1=E1=F3=EA=E5=F5=DE, 14 =D6=E5=E2=F1=EF=F5=DC=F1=E9=EF=F2 2= 003 5:29:34 =EC=EC > To: Louis Godio > Cc: Ioannis Vranos; dev...@li... > Subject: RE: [Dev-C++] recursion > > The factorial is faster and more efficient as a loop. > > In a loop, it may use the input parameter as loop variable, and only need > a temporary for evaluating the product. > > A recursive solution would duplicate the input parameter and a temporary > and a return address for each step of the algorithm. > > That's why faculty is so bad an example when comparing the two solution > methods. While the mathematical definition of the faculty function is > recursive, the actual problem isn't recursive in nature. > > > > =3D=3D> For generic algorithms a recursive solution is always more defici= ent ------For some SIMPLE algorithms, loops may always win. > than the iterative solution. > > In space because every call has its own instances of the local variables = and > also in most cases all the continuous function calls are stored in the st= ack Shared variables area easily stored in a global struct, or in an object. The needed state variables must be kept for a loop solution too, in this case in a queue. > > > In run-time because of the function calls. function call has to be matched against queue push/pop for software-assisted state stack. > > > Ioannis Vranos > > * Programming pages: http://www.noicys.freeurl.com > * Alternative URL: http://run.to/noicys |