Stack is slow than heap?

C

Cholo Lennon

You probably use them all the time, even without realizing. (Quicksort
is a recursive algorithm, and std::sort() usually uses introspective sort,
which uses quicksort as one of its steps.)

I use them from time to time when I need to implement algorithms which
are very recursive in nature. Often implementing them non-recursively
makes the implementation significantly more complicated (and usually
longer) than the recursive version.

Just a comment: Al least in VC++, qsort is implemented for performance
reasons without recursion (well, it use recursion, but using goto and
some stack manipulation)

Regards
 
J

Jorgen Grahn

You probably use them all the time, even without realizing.

Well, I assumed we were talking about "use" as in "write code which
does recursion".
(Quicksort
is a recursive algorithm, and std::sort() usually uses introspective sort,
which uses quicksort as one of its steps.)

I use them from time to time when I need to implement algorithms which
are very recursive in nature. Often implementing them non-recursively
makes the implementation significantly more complicated (and usually
longer) than the recursive version.

I've seen how elegant recursive code can be: my CS education was about
50% functional programming. I should probably use it more often --
but I'd watch out for extreme stack usage.

/Jorgen
 
J

Juha Nieminen

Cholo Lennon said:
Just a comment: Al least in VC++, qsort is implemented for performance
reasons without recursion (well, it use recursion, but using goto and
some stack manipulation)

Technically speaking an algorithm does not become "non-recursive" simply
because it has been implemented without a call to the current function.
It's recursive if it needs a stack whose size depends on the amount of
input. Usually the easiest way to implement this is to simply make the
implementation literally recursive.
 
G

gwowen

  Technically speaking an algorithm does not become "non-recursive" simply
because it has been implemented without a call to the current function.
It's recursive if it needs a stack whose size depends on the amount of
input. Usually the easiest way to implement this is to simply make the
implementation literally recursive.

So consider the following two C99 functions:
void
f1(const int input[], size_t N)
{
int scratch[N];
frob(int,scratch,N);
return;
}

void
f2(const int input[],size_t N)
{
int *scratch = malloc(N * sizeof(int)); // null check elided
frob(input,scratch,N);
free (array);
return;
}

Are you suggesting that f1 is recursive and f2 is not?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top