• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to optimize this code (recursion and iteratorions)

 
vinay basavanal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to edit the link in that last post because http appeared twice in it. It works nicely now
 
vinay basavanal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic