• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to optimize this code (recursion and iteratorions)

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic