优化 DOM 和 XPath Java 代码
我的代码太慢,但我不知道如何改进它。从磁盘读取 1k 文件到 DOM 大约需要 20 毫秒,这可能还不错,具体取决于磁盘,但是我还有另外 20 毫秒来处理 xpath 语句,这太多了。这是一些带有时间注释的示例代码。我该如何改进代码?
这发生在构造时:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = this.dbFactory.newDocumentBuilder();
XPathExpression[] ex = new XPathExpression[]{about 30 different expressions}
XPathExpression mainEx =xPath.compile("/rootElement/firstLevel/secondLevel");
然后代码:
Document doc = this.dBuilder.parse("somefile.xml");
//took 20 ms until here
NodeList nodes = (NodeList) mainEx .evaluate,doc, XPathConstants.NODESET);
//took another 20 ms until here !!!
for (int i = 0; i < nodes.getLength(); i++) {
Node n = nodes.item(i);
for (XPathExpression e:ex) {
String v = (String) e.evaluate(n, XPathConstants.STRING);
if (v != null) {
System.out.println(v);
}
}
}
//this only takes 5 ms
My code is too slow, but I'm not sure how to improve it. Reading from disk into DOM for a 1k-file takes about 20 ms, that might be okay depending on the disk, but then I've got another 20 ms for working on a xpath statement, which is far too much. Here is some sample code with time comments. How can I improve the code?
This happens at construction time:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = this.dbFactory.newDocumentBuilder();
XPathExpression[] ex = new XPathExpression[]{about 30 different expressions}
XPathExpression mainEx =xPath.compile("/rootElement/firstLevel/secondLevel");
Then the code:
Document doc = this.dBuilder.parse("somefile.xml");
//took 20 ms until here
NodeList nodes = (NodeList) mainEx .evaluate,doc, XPathConstants.NODESET);
//took another 20 ms until here !!!
for (int i = 0; i < nodes.getLength(); i++) {
Node n = nodes.item(i);
for (XPathExpression e:ex) {
String v = (String) e.evaluate(n, XPathConstants.STRING);
if (v != null) {
System.out.println(v);
}
}
}
//this only takes 5 ms
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能遇到了我在这里记录的这个问题:
Java XPath(Apache JAXP实现)性能
本质上,您应该添加这些 JVM 参数来大幅加速 Xalan 的 XPath 实现:
或者
You're probably suffering from this problem that I documented here:
Java XPath (Apache JAXP implementation) performance
Essentially, you should add these JVM arguments to heavily speed up Xalan's XPath implementation:
or
您应该将 XPath 表达式预编译为
XPathExpression
,使用XPath.compile
。然后调用XPathExpression.evaluate
。如果您多次执行它,这将节省您的时间。我假设情况确实如此,或者 20 毫秒应该不重要。
编辑:正如评论中提到的,这个 问题 有更多信息,包括JVM 参数。
You should pre-compile your XPath expression to a
XPathExpression
, usingXPath.compile
. Then callXPathExpression.evaluate
.This will save you time if you're executing it more than once. I'm assuming this is the case, or 20 ms shouldn't matter.
EDIT: As mentioned in the comments, this question has further information, including a JVM parameter.