Thread: efficiency question

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    efficiency question

    hello guys its harish again,
    well, when i was working through my project i came across a quick question about the efficiency of the programe which is actually realted to recursion technique. the question is, i got two design to my problem one with the recursion and another iteration tech, i was thinking of to go with the recursion tech, but the problem is if i use recursion i will be using the system stack, compiler complexsity and more complex memory managmnet will be involved while the programe has been executed.

    so can any one tell, is my think is correct and which one is more effecient method to follow with.

    thax in advance

    ssharish2005

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    There is no single correct answer. It depends on the problem your code is written to solve.

    Recursion works well when the depth is limited and you don't anticipate exceptions, and recursive solutions can be much cleaner (code wise) than iterative solutions (less code, fewer conditions). On the other hand, the use of recursion prevents some advanced compiler optimizations from recognizing execution patterns and subtle side effects that might produce more efficient code, and if the depth of the recursion goes too deep, you can start getting page faults on the stack. It is very difficult to parallelize (is that a real word?) a recursive algorithm, which prevents some optimizations available on some architectures.

    Iteration has the advantage of lower call stack overhead at the expense of more complex control structures within the code. Depending on the problem definition and the nature of the recursion required by the algorithm, the data and control structures required to track the state of the processing may introduce too much complexity, and you may lose the advantages of an iterative technique.

    It is up to the programmer to make a choice of algorithms based on the problem definition and the runtime constraints.
    Insert obnoxious but pithy remark here

  3. #3
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    generally recursion is not good solution...but if u show ur code then we might be able to say precisely in ur case

  4. #4
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    Location
    Stockholm
    Posts
    71
    I never use function recursion because it consumes more processing time and resources (the stack) and there is a risk that the stack is blown. Its an abuse in the art of programming a computer to use function recursion, it should have been prohibited in serious programming languages such as C and its dialects.
    Profylaxis defense: Some people has claimed they use function recursion when they can be 100% sure it will not blow the stack. OK so it may work on that particular system, then, lets move that code to another processor, a PIC is an evil choice here because it has only a stack level of 8 function calls, and you no longer can be 100% sure it will not blow the stack. Learning how to do iteration rather than recursion appears to my mind as not any more difficult to grasp, so I see no argument there either and even if it is harder to learn I still see no argument in using function recursion. Therefore I recomend to you and everybody else to use iteration. And ask yourself this: What methode would the system designers of programs used in space shuttles, and medical equipment use, in the choice of function recursion or iteration? What would game designers chose? Or what do you think we will find in top-chess programs?
    Last edited by fischerandom; 12-17-2005 at 11:15 AM.
    Bobby Fischer Live Radio Interviews http://home.att.ne.jp/moon/fischer/

  5. #5
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by fischerandom
    Its an abuse in the art of programming a computer to use function recursion, it should have been prohibited in serious programming languages such as C and its dialects.
    This looks like the foundation of a religious war. When I was in school, recursion was taught as a preferred technique. Of course, that was 25 years ago.

    There are times when I use function recursion in embedded real-time code. Generally, there is a maximum depth on that recursion of 2. You have to, as I said in my previous post, take the run-time environment when choosing the algorithm.
    Insert obnoxious but pithy remark here

  6. #6
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    Keep in mind that functional languages use recursion for "everything". Keep also in mind that sometimes finding the iterative algorithm is very difficult.
    -S

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM