3

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
jasso
  • 13,736
  • 2
  • 36
  • 50
Franz Kafka
  • 10,623
  • 20
  • 93
  • 149

2 Answers2

5

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
Community
  • 1
  • 1
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
4

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.

Community
  • 1
  • 1
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • I changed everything to precompiled expressions, but it doesn't really boost performance, maybe a few ms. Is there some faster way of building the dom or a faster xPath evaluator? – Franz Kafka Nov 27 '11 at 11:27
  • @Franz, so it takes almost 20 ms just to evaluate a previously compiled expression? Which implementation are you using (I think `getClass` will tell you)? This [question](http://stackoverflow.com/questions/6340802/java-xpath-apache-jaxp-implementation-performance) indicates Xalan 2.7.1 is the fastest(of those tested), and provides a JVM parameter for optimization. – Matthew Flaschen Nov 27 '11 at 11:36
  • com.sun.org.apache.xpath.internal.jaxp.XPathImpl, should I switch? – Franz Kafka Nov 27 '11 at 11:38
  • @Franz, I believe that's bundled Xalan. I'm not sure which version, though. Try the JVM parameter from the question I linked. – Matthew Flaschen Nov 27 '11 at 11:41
  • not perfect, but it took of about 8ms. But still evaluating such a basic xPath uses 10ms. – Franz Kafka Nov 27 '11 at 12:06