Forums Register Login

how to optimize this code (recursion and iteratorions)

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
I had to edit the link in that last post because http appeared twice in it. It works nicely now
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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
We're all out of roofs. But we still have tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1453 times.
Similar Threads
lists inside a list :(((
Trees in Java
How to find Path with Max weight
Recursion inside while loop
Tree with parents
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:35:20.