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 ?