문제

Using: Java 1.5 / JDom 1.1.3 / Jaxen 1.1.1

The test I've written was to confirm the belief that using precompiled XPATH in JDOM was faster than iterating through child elements. Instead, what I've found is that XPATH is between 4 and 5 times slower than iterating through lists of children, performing string comparisons, and hunting for what I want.

For context, my XPath is something like:

/root/quote/vehicle[@id = 'some vehicle']/coverage[@id = 'some coverage']/code";

And the actual evaluation being timed (in a try/catch block):

String element = path.valueOf(doc).getText();

And the alternative search is:

List<Element> vehicleList = doc.getRootElement()
                                        .getChild("quote")
                                        .getChildren("vehicle");

for(Element vehElement : vehicleList)
    if(vehElement.getAttributeValue("id").equals("some vehicle")){
        List<Element> coverageList = ele.getChildren("coverage");
        for(Element covElement : coverageList){
            if(covElement.getAttributeValue("id").equals("some coverage")){
                element = covElement.getChild("CoverageType").getText();
                break;
            }
        }
    }

Curiously, while the runtime of the method using XPATH is much slower, it is most consistent over 1000 iterations.

The first example completes around .29 ms +- 0.01ms.

The second example completes anywhere between .013ms and .002ms.

Both approach very short running times given a long enough test.

The XPath is, for me, easier to write, however the getChild route seems more flexible but a little verbose. Still that's a trade I don't mind making for speed. It is also true that even 100 iterations is incredibly fast, so this may be academic...

Ultimately I'd like to know:

Is there a scenario where JDOM Xpath is faster than the alternative style shown ?

What benefits does JDom XPath (in any version of Java/JDOM) bring ?

도움이 되었습니까?

해결책

There are a few things to note in here.... I have done (I'm a JDOM maintainer) extensive work on JDOM 2.0.1 especially in regards to performance of XPath evaluation. Here are some numbers:

http://hunterhacker.github.com/jdom/jdom2/performance.html

Read it from the bottom up.

Here are some other interesting numbers (compares different JDOM versions with different Java VM's)

http://hunterhacker.github.com/jdom/jdom2/performanceJDK.html

The 'bottom line'....

  • JDOM 2.x introduces faster iterators. Jaxen is very Iterator intensive, and the performance improvements in JDOM 2.x are significant in this regard....
  • Java 7 is much faster than previous versions in regard to iterator performance too.
  • There is no benefit to 'compiling' Jaxen XPaths....
  • even in the best of times though, the 'native' method of searching will be faster than the XPath version.

Your biggest performance boost will come from running with Java7, then upgrading to JDOM 2.x

Although the 'custom' search, if written efficiently, will always be faster than XPath.

Edit: Also, JDOM 2.x introduces a new API for running XPath queries that you may find easier to work with (although the old API still works too): https://github.com/hunterhacker/jdom/wiki/JDOM2-Feature-XPath-Upgrade

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top