Re: Recursive Functions
From: Julian V. Noble (jvn_at_nowhere.virginia.edu)
Date: 10/30/03
- Next message: Keith Thompson: "Re: Linked list trouble..."
- Previous message: Micah Cowan: "Re: C++ to C"
- In reply to: Richard Heathfield: "Re: Recursive Functions"
- Next in thread: E. Robert Tisdale: "Re: Recursive Functions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 29 Oct 2003 22:51:10 -0500
Richard Heathfield wrote:
>
> Glen Herrmannsfeldt wrote:
>
> >
> > "Marcus Lessard" <spam08@spam.com> wrote in message
> > news:bnlrqm$qg2$1@pyrite.mv.net...
> >> Maybe it's just me but doesn't the contrived nature of the function
> >> scream
> >> out "Homework Assignment?" Maybe I'm missing something but I just can't
> > see
> >> why you'd ever want to compute this value..
> >
> > Computing integer powers of numbers is fairly common in scientific
> > programming, and is one of the relatively few things missing from C used
> > in scientific programming.
> >
> > However, requiring it as a recursive function does scream of homework. A
> > simple for loop should be easier and faster.
>
> Hmmm. I raised 7 to the power 7777 using recursion and iteration. (Since the
> result occupies over 6500 decimal digits, I won't display it here!)
>
> The recursive calculation took 0.22 seconds, and the iterative version 1.06
> seconds - almost five times slower. Perhaps you could demonstrate an
> iterative version that can hold a candle to the recursive technique?
>
> --
> Richard Heathfield : binary@eton.powernet.co.uk
> "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
> C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
> K&R answers, C books, etc: http://users.powernet.co.uk/eton
To compare recursive and iterative versions you have to know what is
happening at the machine level. Some processors do recursion faster
because they can parallelize stack ops with arithmetic ops.
I found that the recursive version of the extended Euclid algorithm
for greatest common divisor runs 20-30% faster than the iterative
version given by Knuth, on Pentium-class machines.
I expect this would not be true on machines with enough registers that
all the intermediate results could be allocated to registers.
--
Julian V. Noble
Professor Emeritus of Physics
jvn@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/
"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
- Next message: Keith Thompson: "Re: Linked list trouble..."
- Previous message: Micah Cowan: "Re: C++ to C"
- In reply to: Richard Heathfield: "Re: Recursive Functions"
- Next in thread: E. Robert Tisdale: "Re: Recursive Functions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|