IntelliJ Java IDE
The moose likes Performance and the fly likes Avoiding Recursion Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Performance
Reply Bookmark "Avoiding Recursion" Watch "Avoiding Recursion" New topic
Author

Avoiding Recursion

Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
Hi,
I just learned that posts in the book giveaway thread are ineligible, so I'm making a post here.

Although it's not always possible, I always try to avoid recursion because iteration is almost always faster.
In looping constructs, I sometimes avoid calculating a length each time:

Java performance techniques work with javascript and have a more pronounced effect because javascript is much slower.


comp.lang.javascript FAQ: http://jibbering.com/faq/
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Garrett Smith:
Although it's not always possible, I always try to avoid recursion because iteration is almost always faster.

And almost always, it probably doesn't matter - seriously.
I would *always* go with the solution that is easier to read (more intuitive) first and only think about reworking a recursion into iteration if profiling showed that it was a problem. Remember, the biggest cost factor in our industry is maintenance!

In looping constructs, I sometimes avoid calculating a length each time:


In my opinion using an Iterator to encapsulate the looping logic might be more appropriate.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jack Shirazi
Author
Ranch Hand

Joined: Oct 26, 2000
Posts: 96
Loads of the tuning techniques I cover are applicable to many languages. Over the years I have picked up techniques from other languages too. Lot's of the low level ones have inevitably been tried in C. Many of the OO ones were used in Smalltalk. Just as you don't restrict yourself to looking at only javascript specifc tips, I don't restrict myself to looking at Java performance techniques either. And I'm sure we're both the richer for that.
--Jack Shirazi
JavaPerformanceTuning.com
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
Yeah, I think it's how you design a program that can make a huge difference. Everything from the general program concept and approach to the algorithms, to the really language-specific stuff.

I would *always* go with the solution that is easier to read (more intuitive)

Sometimes recursive solutions are easier to spot, and if I think about it, I can sometimes find an iterative solution that is just as concise.
When I do this, I always choose the iterative solution.
Sometimes I don't see it right away. The more I do this, the easier it becomes.
I think Ilja's advice is good advice because it tends to lead to self-documenting code.
In some conditions, the differences between iteration and recursion may be noticeable. This is more often true (and to a greater degree) with javascript.
As for looking into other languages, I actually do program in Java, just not professionally. I'd like to get a job. Hey, then I could even afford to buy Jack's book!
 
 
subject: Avoiding Recursion
 
Threads others viewed
PhraseOMatic syntax problem in Head First Java
A cloven question:Servlet dual output
JTable
String compression
Is my prof trolling me? (factorials, loops)
MyEclipse, The Clear Choice