example program
  Home FAQ Contact Sign in
comp.lang.c only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c Profile…
 Up
example program         


Author: aarklon
Date: Jun 2, 2008 03:29

Hi all,

can any one give a example program where recursive version is faster
than iterative version ?
23 Comments
Re: example program         


Author: Bartc
Date: Jun 2, 2008 03:44

aarklon@gmail.com wrote:
> Hi all,
>
> can any one give a example program where recursive version is faster
> than iterative version ?

For trivial programs there may or may not be examples where recursion is
faster.

But recursion is a natural fit for many kinds of programming which would be
a pain to implement with iterative methods.

Especially with making arrangements to save/restore complex data which may
well end up slower than just using recursion. But even if recursion was
slower, the difference would be minimal in a real application, while keeping
the code much cleaner.

--
Bartc
no comments
Re: example program         


Author: Richard Heathfield
Date: Jun 2, 2008 04:43

Bartc said:
> aarklon@gmail.com wrote:
>> Hi all,
>>
>> can any one give a example program where recursive version is faster
>> than iterative version ?
>
> For trivial programs there may or may not be examples where recursion is
> faster.

There is, however, no shortage of examples where recursion is /slower/.
> But recursion is a natural fit for many kinds of programming which would
> be a pain to implement with iterative methods.

Right. We don't recurse for speed, but for clarity (where it /is/ clearer)
- and even then only if the cost in terms of speed loss is more than
adequately compensated by the gain in clarity.

To take a famous example, the following code:
Show full article (1.31Kb)
no comments
Re: example program         


Author: Barry Schwarz
Date: Jun 2, 2008 04:53

On Mon, 2 Jun 2008 03:29:17 -0700 (PDT), aarklon@gmail.com wrote:
>Hi all,
>
>can any one give a example program where recursive version is faster
>than iterative version ?

Primary concern: You need two versions of the program. How do you
confirm they are truly equivalent and that each is really coded for
highest efficiency?

Secondary concerns: On which hardware? Using which operating system?
Using which compiler? With what options? Which measure of speed, CPU
time or wall clock?

Are you getting the hint that there is no general answer?

Remove del for email
no comments
Re: example program         


Author: rahul
Date: Jun 2, 2008 04:57

Recursive programs can be faster than the iterative versions when the
only changes you have introduced in the iterative version is saving
and restoring states. The system is of course faster in performing
push and pop as it simply means issuing 1 or 2 machine instructions.
In case of factorials, the stack frame is completely unnecessary. So
the iterative version is magnitudes faster than the recursive one.

Towers of Hanoi is faster in recursive version than the iterative one.
I am not sure about the quick sort. I will have to profile it but
surely the recursive version is more clear than the iterative one.
no comments
Re: example program         


Author: pete
Date: Jun 2, 2008 06:10

aarklon@gmail.com wrote:
> Hi all,
>
> can any one give a example program where recursive version is faster
> than iterative version ?

Iterative mergesorts that I've seen for arrays,
tend to split less evenly than the recursive versions do.

When I race array sorting functions,
I can't get any speed from the iterative mergesorts.

I still haven't figured out how to mergesort a linked list iteratively.

--
pete
no comments
Re: example program         


Author: Eric Sosman
Date: Jun 2, 2008 06:28

pete wrote:
> aarklon@gmail.com wrote:
>> Hi all,
>>
>> can any one give a example program where recursive version is faster
>> than iterative version ?
>
> Iterative mergesorts that I've seen for arrays,
> tend to split less evenly than the recursive versions do.
>
> When I race array sorting functions,
> I can't get any speed from the iterative mergesorts.
>
> I still haven't figured out how to mergesort a linked list iteratively.

See the thread "Mergesort algorithm for linked lists"
from January 2007 in this newsgroup.

--
Eric Sosman
esosman@ieee-dot-org.invalid
no comments
Re: example program         


Author: vamsi.krishnak
Date: Jun 2, 2008 08:47

> can any one give a example program where recursive version is faster
> than iterative version ?

Try doing a BFS (Breadth First Search) or DFS of a dense graph
recursively
and iteratively (using lists) you will see a lot of difference.

Thanks,
Vamsi Kundet.
no comments
Re: example program         


Author: Paul Hsieh
Date: Jun 2, 2008 09:57

On Jun 2, 3:29 am, aark...@gmail.com wrote:
> Can any one give a example program where recursive version is faster
> than iterative version ?

I can think of two:

1) Return the ith term of a fibonacci.



No, seriously. Its a matter of *WHICH* recursive formula you use.
There is a quadratic formula for {fib(n+1),fib(n)} in terms of
{fib(floor(n/2)+1),fib(floor(n/2))} which means that you can calculate
fib(n) in O(log(n)) steps (using recursion) instead of O(n) steps as
is usually the case with the typical iterative solution. For very
large n in big integer systems this is, in fact, that fastest
algorithm I know of for calculating fib(n). Anywhere where the direct
exponential formula can be used (when n is relatively small), tables
can also be used and are tremendously faster.
Show full article (1.33Kb)
no comments
Re: example program         


Author: CBFalconer
Date: Jun 2, 2008 13:30

pete wrote:
> aarklon@gmail.com wrote:
>>
>> can any one give a example program where recursive version is
>> faster than iterative version ?
>
> Iterative mergesorts that I've seen for arrays, tend to split
> less evenly than the recursive versions do. When I race array
> sorting functions, I can't get any speed from the iterative
> mergesorts. I still haven't figured out how to mergesort a
> linked list iteratively.

I posted a complete linked list mergesort here one or two weeks
ago.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
no comments

RELATED THREADS
SubjectArticles qty Group
2008/04/16 new programs and new trading programs addedalt.cracks ·
1 2 3