title: When is JDOM XPATH faster than element hunting with getChildren?
body: <p><em>Using: Java 1.5 / JDom 1.1.3 / Jaxen 1.1.1</em></p>
  
  <p>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.</p>
  
  <p>For context, my XPath is something like:</p>
  
  <pre><code>/root/quote/vehicle[@id = 'some vehicle']/coverage[@id = 'some coverage']/code";
  </code></pre>
  
  <p>And the actual evaluation being timed (in a try/catch block):</p>
  
  <pre><code>String element = path.valueOf(doc).getText();
  </code></pre>
  
  <p>And the alternative search is:</p>
  
  <pre><code>List&lt;Element&gt; vehicleList = doc.getRootElement()
                                          .getChild("quote")
                                          .getChildren("vehicle");
  
  for(Element vehElement : vehicleList)
      if(vehElement.getAttributeValue("id").equals("some vehicle")){
          List&lt;Element&gt; coverageList = ele.getChildren("coverage");
          for(Element covElement : coverageList){
              if(covElement.getAttributeValue("id").equals("some coverage")){
                  element = covElement.getChild("CoverageType").getText();
                  break;
              }
          }
      }
  </code></pre>
  
  <p>Curiously, while the runtime of the method using XPATH is much slower, it is most consistent over 1000 iterations.</p>
  
  <p>The first example completes around .29 ms +- 0.01ms.</p>
  
  <p>The second example completes anywhere between .013ms and .002ms. </p>
  
  <p>Both approach very short running times given a long enough test.</p>
  
  <p>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...</p>
  
  <p>Ultimately I'd like to know:</p>
  
  <p><strong>Is there a scenario where JDOM Xpath is faster than the alternative style shown ?</strong></p>
  
  <p><strong>What benefits does JDom XPath (in any version of Java/JDOM) bring ?</strong></p>