Advertisement

How much of a performance hit do recursive functions have?

Started by November 11, 2000 01:06 PM
79 comments, last by cearny 24 years, 1 month ago
I''ve read that recursive functions have quit e an impact on the performance. In real-life game programming, is it worth it to make your source code unreadable and do only iterative finctions instead of recursive ones. I am currently working on a terrain engine using ROAM. Is it worth it so make the splitting functions iterative? Thanks, Adrian
[ Libraries - STLport | boost | SDL | wxWindows ]
[ Manuals - MSDN | STL Docs ]
[ Compilers - VS.NET | MingW | DJGPP ]
[ Editors/Tools - EditPlus 2 | Anjuta | Dev-C++ ]
Unless it is absolutely, positively im-bleepin-possible to not use recursive function calls, don''t.

that doesn''t mean you can''t use recusive technique, just dont have a function call itself.
use whiles and loop until the terminating case is found, don''t do a recursive function call (xsp on trees!).
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
With recursive calls you''ll lose as much time as it takes to pass all the arguments on the stack, save states of registers, etc. This can cost quite a bit if the functions are getting called often.

-Ironblayde
 Aeon Software

Next thing you know, they''ll take my thoughts away.
"Your superior intellect is no match for our puny weapons!"
Hmm... That disturbs me a bit.. I find recursive programming techniques to often be my saving grace. I wasn''t aware that its that bad.. I assumed that since its widely regarded as a fundamental concept that it was somehow efficient (strange logic i know...).

If it can really bog an app down, can someone give an example of something that is logically recursive, implemented iteratively?

Take for instance the given example of ROAM splitting function. How would you do that iteratively?


--------------------------
I guess this is where most people put a famous quote...
"Everything is funnier with monkey''''s" - Unknown
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
Recursion is very dangerious,

From the people I''ve talked to in the industry it leads to many problems. For one you could overflow your programs'' stack, you could terminate your program while it still is executing a recursive call, and a host of other problems. Iterative is the way to go. Yeah, it''s hard to read, but it''s faster and more stable than recursion. That''s what angers me about most computer science departments. They teach all of this recursion, but it''s not used in industry. My first C++ teacher was a networker who lost a bet, and had to teach a night class. He was one of my best teachers. His opinion on recursion was a slight chuckle, and a change of subject.
a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};

Why is it called a hot water heater? Isn't it cold when it goes in the tank?

[email=jtaylor@gtemail.net" class="h]-=CF=-[/email]
Most computer science departments tout recursion as being great because their hoity-toity research languages ONLY do things recursively. Languages like Scheme, ML, Lisp , and Prolog (my profs fav. language) make things impossible to do iterative solutions (or to have two statements in a row). The aforementioned languages are not Procedural like C++ or Java though, which is why you can''t have iterative solutions.

Recursion has its place, just not anywhere outside researching machine learning.
Advertisement
quote:
Recursion has its place, just not anywhere outside researching machine learning.


Well if your doing your loops at compile time, i.e. with templates, you don't have a choice but to use recursion. But the good side is that it's geet fast cos by the time your program is running all your recursive template looping dohickity has been replaced with a constant value

Although I've only used this once in real code (and it was an optimisation!), so you could still call it research

Edited by - Wilka on November 11, 2000 6:17:12 PM
What a bunch of crap here.

Recursion is so fundamental to hard problems it is unavoidable. My guess is all the people touting recursion is bad are the same ones who have never programmed a complex system.

Do ROAM without recursion.
Do compiler design without recursion.
Do interpreters without recursion.
Do raytracing without recursion.
Do numerical analysis without recursion.
Do automated foliage generation without recursion.
Calculate the contributing area to a cell from a grid for water flow dynamics without recursion.
Do Hierarchical Radiosity without recursion.

Come back here and implement these concepts without recursion and then say recursion should not be used. Then I'll buy your comments.

Oh, is it that I am hearing that you only do that stuff in computer science classes? Is that your line of argument?



Edited by - bishop_pass on November 11, 2000 7:10:40 PM
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
I think he was mostly talking about recursion in games. In regular apps it doesn''t really matter, in fact, some things really need recursion. Some sorts need (require is such a finalizing word) recursion. In games it may not be absolutely necessary. Just like you don''t typically use to many global variables in apps, but sometimes can''t be avoided in games w/o hours of killer headaches.


"We are the music makers, and we are the dreamers of the dreams."
- Willy Wonka
That''s probably true. However the original poster (cearny) was trying to decide whether or not to implement ROAM iteratively or recursively.

I feel a little better knowing that someone else besides me doesn''t think recursion is all evil, but I''m still curious if it is possible to implement the ROAM split as an iterative function. I don''t really see how to do it.

Anyone want to demonstrate with some example code?

--------------------------
I guess this is where most people put a famous quote...
"Everything is funnier with monkey''''s" - Unknown
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown

This topic is closed to new replies.

Advertisement