jdom performance

2019-07-20 20:04发布

问题:

I am currently using native Java XML processing library (Xerces). I can't use any sax parser because I need random access to xml nodes. I am finding that CPU usage goes 100% when I am parsing XML files. There are large number of small size (1-10kb) that I am processing like this-

while(hasFile){
processXMlfile(hasFile.next);
}

In processXMlfile() I am building parsing and processing file.

If I move to JDOM library, will I gain any performance benefit?

回答1:

The bottleneck is probably XML parsing, and JDOM will likely use the same XML parser under the covers, so it won't make any difference.

A key factor when you are parsing lots of small files is to avoid the parser initialization costs. Reuse the same XML parser instance for all the files.



回答2:

JDOM 2.0.0 addresses a number of performance issues. One of them is directly related to the 'tight loop' processing of XML files.

Have a look at:

http://hunterhacker.github.com/jdom/jdom2/apidocs/org/jdom2/input/sax/package-summary.html

You should do something like:

SAXBuilder saxbuilder = new SAXBuilder();
saxbuilder.setVariousConfigurations()
SAXEngine saxengine = saxbuilder.buildEnine();

while(hasfile) {
  processXML(saxengine, nextfile);
}

Using the SAXEngine concept in JDOM 2.0.0 will completely eliminate the setup of the SAX-parsing infrastructure. That setup time accounts for a huge proprotion of overall procesing.

While you may still be running at 100%, yu will find your throughput will likely more than double....