aspose file tools*
The moose likes Performance and the fly likes how more i can optimize this code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "how more i can optimize this code?" Watch "how more i can optimize this code?" New topic
Author

how more i can optimize this code?

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2
i can modify only this part:


how can i increase the performance of this code?
any ideas, i want to save as much as time i can.
rohit chavan
Ranch Hand

Joined: Oct 08, 2010
Posts: 132

Can you share some more info, like

1. What are you aiming to do.
2. The size of the input XML
3. What is the benchmark for it (like currently it take this much time for this much data on this this version of java)


jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 296

Hi Punit,

Agree with Punit,
Seems good but jst curious to know "have you thought of the cleaning of the objects like assigning the null reference so they can be eligible for the garbage collection?" And also eager if someone can advice on this code..

Regards
Jatan

Frank Pavageau
Greenhorn

Joined: Jul 20, 2010
Posts: 10
This looks like an assignment, but: don't create (and set up) the Transformer in the loop, it's an invariant and supposed to be reused.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

jatan bhavsar wrote:"have you thought of the cleaning of the objects like assigning the null reference so they can be eligible for the garbage collection?"

No need. Once something is out of scope it's eligible.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Creating the StringWriter with an initial size larger than your expected transformed output will save some StringBuffer expansion steps.

Expansion requires memory allocation and arraycopy operations each time.

A plain StringWriter starts with only a 16 character buffer.

Bill
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2

2. The size of the input XML

app 9 mb.


3. What is the benchmark for it (like currently it take this much time for this much data on this this version of java)

16.1 ms for each iteration.

1. What are you aiming to do.

basically fetching data from xml depends to the xpath.
one xml file is of 9mb which has all the data, and one xml file has xpaths, so fetching data as per the xpaths.


Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2
jatan bhavsar wrote:Hi Punit,

Agree with Punit,from the loop
Seems good but jst curious to know "have you thought of the cleaning of the objects like assigning the null reference so they can be eligible for the garbage collection?" And also eager if someone can advice on this code..

Regards
Jatan



don't think it will do anything, because there scope is inside the loop, and will gc as they out from the loop.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
Well, much like Frank noted for the Transformer, there's probably no need to create a StringWriter and StreamResult each time through the loop, either. You're creating StringWriters just to create Strings that are appended to a StringBuilder, to make a String. Try to just do all that with just one StringWriter created once at the beginning of the loop (with a suitable size as Bill suggests), and you will spend less time copying bytes from one string to another.
rohit chavan
Ranch Hand

Joined: Oct 08, 2010
Posts: 132

Agree with Mike.
"Take object creation out of the loop" seems to be the best thing to do, for optimization, as far as this piece of code is concerned.

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Note that although XPATH is great for clear expression of a request and simple code, it is nowhere near as fast as using the DOM methods directly.

Bill

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how more i can optimize this code?