You are here:

C/Recursion vs Iteration

Advertisement


Question
When i perform a task using either recursion or iteration(looping) (for Ex: printing of Numbers), which one will be fast and why? What might be the strong points to support?


Answer
Hi Giri

1000 out of 1000 time iteration is better than recursion the reasons are listed below:

1. Calling a function requires some overhead like space and time as we all know that when a function is called the present function is pushed into the stack and the control passes over to the called function this requires more time as well as space.

2. You can't have any infinite recursion for example
void func()
{
 printf("\nHello World")
 func()
} This is not an infinite recursion coz this will print Hello World until the stack overflows so this easily proves that recursion eats more time and space compared to iteration.

Hope that this will clear all your doubts but still in case if you stuck at any place please feel free to contact me.

Regards
Joydeep  

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joydeep Bhattacharya

Expertise

Ability to solve C and Data Structure problems and puzzles with simple and easy to understand logic.

Experience

C, C++, Java, Data Structure, PHP, Web Designing

Organizations
http://www.scodz.com Designation: webmaster

Publications
http://www.scodz.com

Education/Credentials
Master of Computer Applications

Past/Present Clients
http://analysingc.50webs.com http://www.funforu.com

©2012 About.com, a part of The New York Times Company. All rights reserved.