Reputation: 10841
My code is too slow, but I'm not sure how to improve it. Reading from disk into DOM for a 1k-file takes about 20 ms, that might be okay depending on the disk, but then I've got another 20 ms for working on a xpath statement, which is far too much. Here is some sample code with time comments. How can I improve the code?
This happens at construction time:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = this.dbFactory.newDocumentBuilder();
XPathExpression[] ex = new XPathExpression[]{about 30 different expressions}
XPathExpression mainEx =xPath.compile("/rootElement/firstLevel/secondLevel");
Then the code:
Document doc = this.dBuilder.parse("somefile.xml");
//took 20 ms until here
NodeList nodes = (NodeList) mainEx .evaluate,doc, XPathConstants.NODESET);
//took another 20 ms until here !!!
for (int i = 0; i < nodes.getLength(); i++) {
Node n = nodes.item(i);
for (XPathExpression e:ex) {
String v = (String) e.evaluate(n, XPathConstants.STRING);
if (v != null) {
System.out.println(v);
}
}
}
//this only takes 5 ms
Upvotes: 3
Views: 5894
Reputation: 221145
You're probably suffering from this problem that I documented here:
Java XPath (Apache JAXP implementation) performance
Essentially, you should add these JVM arguments to heavily speed up Xalan's XPath implementation:
-Dorg.apache.xml.dtm.DTMManager=
org.apache.xml.dtm.ref.DTMManagerDefault
or
-Dcom.sun.org.apache.xml.internal.dtm.DTMManager=
com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault
Upvotes: 5
Reputation: 284927
You should pre-compile your XPath expression to a XPathExpression
, using XPath.compile
. Then call XPathExpression.evaluate
.
This will save you time if you're executing it more than once. I'm assuming this is the case, or 20 ms shouldn't matter.
EDIT: As mentioned in the comments, this question has further information, including a JVM parameter.
Upvotes: 4