Skip to content

Instantly share code, notes, and snippets.

@akhikhl
Created May 30, 2014 09:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhikhl/c784bb2170b6c0c8df7e to your computer and use it in GitHub Desktop.
Save akhikhl/c784bb2170b6c0c8df7e to your computer and use it in GitHub Desktop.
XmlSlurper vs JDOM2 performance
@Grab('org.jdom:jdom2:2.0.5')
@Grab('xerces:xercesImpl:2.11.0')
@Grab('xml-apis:xml-apis:1.4.01')
@Grab('jaxen:jaxen:1.1.4')
@GrabExclude(group='jdom', module='jdom')
@GrabExclude(group='org.jdom', module='jdom')
import org.jdom2.Document
import org.jdom2.Element
import org.jdom2.JDOMException
import org.jdom2.input.SAXBuilder
import org.jdom2.filter.ElementFilter
import org.jdom2.xpath.XPathExpression
import org.jdom2.xpath.XPathFactory
def path = '/home/ahi/Downloads/sample_xml.xml'
int count = 50
def data = []
count.times {
data.add([0, 0, 0, 0])
}
// == XmlSlurper
count.times { i ->
print '.'
long startTime = System.currentTimeMillis()
def xml = new XmlSlurper().parse(path)
data[i][0] = System.currentTimeMillis() - startTime
startTime = System.currentTimeMillis()
def c_elem_count = xml.'**'.findAll { it.name() =~ 'c[0-9]+' }.size()
data[i][2] = System.currentTimeMillis() - startTime
}
println()
// == JDOM2
def xpath = XPathFactory.instance().compile('//*[starts-with(name(), "c")]', new ElementFilter(), [:], [])
count.times { i ->
print '.'
long startTime = System.currentTimeMillis()
def xml = new SAXBuilder().build(path)
data[i][1] = System.currentTimeMillis() - startTime
startTime = System.currentTimeMillis()
def c_elem_count = xpath.evaluate(xml)?.size()
data[i][3] = System.currentTimeMillis() - startTime
}
println()
println 'XmlSlurper parse, JDOM2 parse'
count.times { i ->
println data[i][0..1].join(', ')
}
println 'XmlSlurper findAll, JDOM2 findAll'
count.times { i ->
println data[i][2..3].join(', ')
}
@akhikhl
Copy link
Author

akhikhl commented May 30, 2014

The results for parsing 60MB XML file on i7 machine with 8GB memory and Linux Mint 64-bit:

https://dl.dropboxusercontent.com/u/15089387/XmlSlurper_vs_JDOM2/parse.ods

https://dl.dropboxusercontent.com/u/15089387/XmlSlurper_vs_JDOM2/iterate.ods

Interpretation:

  • JDOM2 parses large files approx. 15% faster than XmlSlurper
  • XmlSlurper iterates over large XML file 45% faster than JDOM2/XPath

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment