【发布时间】:2014-08-30 17:21:20
【问题描述】:
我只需要检查每个文件(109 个文件)中的一个节点,它们存储在不同的 url(109 个 url)上。 我用这个代码
public class XPathParserXML {
public String version(String link, String serial) throws SAXException, IOException,
ParserConfigurationException, XPathExpressionException{
String version = new String();
String url = link+serial;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(url);
XPath xPathFactory = XPathFactory.newInstance().newXPath();
XPathExpression expr = xPathFactory.compile("//swVersion/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList node = (NodeList) result;
if (node == null){
version = "!!WORKING!!";
}else{
version = node.item(0).getNodeValue();
}
return version;
}
}
我在 cicle 中调用了“version(link,serial)”方法 109 次
我的代码需要大约 20 秒来详细说明。每个文件重 0.64KB,我有一个 20MB 的连接。
我可以做些什么来加快我的代码速度?
【问题讨论】:
-
并行下载?
-
您是否对您的应用程序进行了概要分析?你的大部分表现在哪里丢失?
-
@Lukas 我将 Eclipse 与 JVM 监视器一起使用。当我调用方法“版本”时,我的应用程序 (.jar) 需要 20-25 秒来加载我需要的所有数据。我的应用程序是单线程那一刻
-
但是那个方法需要这么长时间?解析? HTTP 连接? XPath 评估?
-
嗯,你有运行 XML 验证器吗?这似乎异常缓慢......我会用另一种选择来更新我的答案。