improving program execution time

do recursive programs are less time taking than iterative ones??

definitely no, in languages like c++ where function calls are expensive.

I think it depends what problem you want to solve. Typically (my experience) it is easier to write recursive version, than iterative one, assume DFS as an example…

1 Like


Basically iterative ones are faster than recursive calls, because the function calls take some time compared to iterative ones where there is no function calls(but recursive functions are easier to write compared to iterative ones :slight_smile: )

Helpful links :

Wishes :slight_smile:

in c/c++, function call is expensive? i didnt know

nice posts.

nice post…thanx…

I think Recursive Programs are more taking than iterrative ones.

thanx …nice post…

@admin, ban this retard.

I agree with @betlista that writing a recursive function is, more often than not, more elegant. A recursive function also has the advantage of keeping the programmer at an abstract level, and avoid drowning him in implementation details. He/She can then focus on breaking the problem into smaller ones without getting a headache from ‘while loops’.

But is a ‘while loop’ such a big headache? Recursion guarantees neither simplicity nor ease. Each line of recursive code requires deeper insight. Debugging is usually more painful, and the space/time required to allocate stack memory for each function call can make it infeasible.

It would be better if you check the problem constraints before deciding your final approach. At least, you can quickly work out the recursive function on paper. Dynamic programming and tail recursion in Scala can reduce the time required for a recursive algorithm tremendously. (Notice that Scala internally converts a recursive function to an iterative one, if you are nice enough to mention “@tail-recursive”.)

So, the answer is: no. Recursive algorithms usually take more time than iterative ones. But it is better to look at the situation before adopting/dumping it.

Best of Luck. :slight_smile:

1 Like

I agree with betlista and gkcs, recursive implementation isn’t always fast, rather it also requires stack memory so multiple recursive calls may result in exhausting of memory and regular function calls also requires transfer of flow of control so regular calls results in increased execution time…

Time expensed in multiple calls to recursive function which occurs due to transfer of control can be saved by making the function inline… inline function are faster in C/C++, but there definition should be small… for ex: inline recursive function for fibonacii series

1 Like

No, there is seldom a use for inlining function manually. The Compiler decides whether a function is to be inlined or not. Writing inline is just a hint. I think gcc/g++ doen’t care about this hint at all.