wood burning stoves*
The moose likes Performance and the fly likes how to optimize this code (recursion and iteratorions) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "how to optimize this code (recursion and iteratorions)" Watch "how to optimize this code (recursion and iteratorions)" New topic
Author

how to optimize this code (recursion and iteratorions)

vinay basavanal
Greenhorn

Joined: Dec 08, 2012
Posts: 8
I have written some code which will fetch contents from a resource which is actually stored in a tree format. Since it is in a tree format, there will be a parent-child relation and hence recursion.

I am facing lot of performance issue as tree is growing in size this particular piece of code takes up-to 25 sec which is very bad. This piece of code will basically read data stored in file system (just for example) each content has certain set of property which it has to read.



I know code is not complete and may be in appropriate to review but I can't paste the whole of my code hence I hope you understand and review there is recursion and iteration any way I can better write this piece of code.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

The first step in doing optimization is always to use a profiler to determine where it is slowing down.

Sure, you can look, and you can guess...but almost every time, you will guess wrong.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

Are you looking for a Fork/Join solution? F/J works well with recursion. Perhaps this article I wrote may help: A Java Fork-Join Conqueror


Ed's latest article: A Java Parallel Calamity http://coopsoft.com/ar/Calamity2Article.html
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38040
    
  22
I had to edit the link in that last post because http appeared twice in it. It works nicely now
vinay basavanal
Greenhorn

Joined: Dec 08, 2012
Posts: 8
fred rosenberger wrote:The first step in doing optimization is always to use a profiler to determine where it is slowing down.

Sure, you can look, and you can guess...but almost every time, you will guess wrong.



populateLinks is the method which approximately takes about 70% of time i.e about 16 sec .It has recursion and iteration how can i optimize this.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

vinay basavanal wrote:

You need to get rid of this. If something goes wrong, you won't even know about it.

It might even be the reason for bad performance: it is possible that your code throws a ton of exceptions, but you don't know about them. Since handling an exception is fairly expensive, having lots of them could easily slow the program down considerably (apart from giving wrong results because of the ignored exceptions, of course). This is just a guess. I don't know if your code throws any exceptions. But you don't know either

I would also try to avoid creating new locale again and again. In your case you should probably just use Locale.US.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

vinay basavanal wrote:I have written some code which will fetch contents from a resource which is actually stored in a tree format. Since it is in a tree format, there will be a parent-child relation and hence recursion.
I am facing lot of performance issue as tree is growing in size this particular piece of code takes up-to 25 sec which is very bad. This piece of code will basically read data stored in file system (just for example) each content has certain set of property which it has to read.

Well, I can't be absolutely sure, but that seems to be an awful lot of procedure to convert something that, presumably, is already in a pseudo-tree format.

My suggestion: StopCoding (←click) and write down exactly how this mapping is supposed to work (ie, what you're trying to do).

I think, if it was me, I'd also make my Links class more atomic; perhaps something like:which would allow you to build up each link incrementally.

I also worry a bit that the link doesn't contain a value other than its name (unless the name is the value). In general, node structures are used to store values in tree form and so generally have the form Node<T> (where T is the type of value stored), whereas all your class seems to do is store other nodes.

But it's quite possible I'm missing something.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to optimize this code (recursion and iteratorions)
 
Similar Threads
Tree with parents
lists inside a list :(((
How to find Path with Max weight
Trees in Java
Recursion inside while loop