Created
May 30, 2014 09:26
-
-
Save akhikhl/c784bb2170b6c0c8df7e to your computer and use it in GitHub Desktop.
XmlSlurper vs JDOM2 performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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(', ') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: