几个月前我刚学会编码,但我的项目实际上对于我已经知道的来说非常繁重,如果能帮助我提高代码运行效率,我们将不胜感激。
我想做的是让这段代码更有效率,因为处理一个 30 MB 的文件需要 20 个小时,而我想处理一个 6.5 GB 的文件。我需要它最多在 30 分钟内处理文件……这可能吗?
我在代码中所做的是:
- 我提取一个单词并检查它的 ID 是否存储在我的 HashMap 中
- 我得到这个词的所有父项并将它们添加到列表中
- 在列表的每一项中,我得到 ID 和 Word 以及其他 parent
- 我创建一个节点并将其添加到 HashMap 中
- 然后继续下一个单词
附言我不知道如何编写 Hadoop MapReduce 代码,我知道这是显而易见的解决方案……但我没有时间学习它。
更新!! [如您在屏幕截图中所见,99.7% 的时间用于从 WordNet 词典中“获取实例”,这是我正在使用的库:extjWNl。 “getResourceInstance”是调用字典本身的方法,第三个突出显示的条目是我调用这些方法的方法(其余方法实际上只花费了 0.001% 的时间)
我不确定这个问题是否可以解决,或者您有什么想法? - 单击此“1”获取屏幕截图] 1
static HashMap<Long, Node> graph = new HashMap <Long, Node> ();
private static void demonstrateTree (IndexWord word) throws JWNLException {
Long Os = word.getSenses().get(0).getOffset();
if (graph.containsKey(Os)) {
return;
}
PointerTargetTree hypernyms = PointerUtils.getHypernymTree(word.getSenses().get(0));
List<PointerTargetNodeList> hypernymsList = hypernyms.toList();
for(int c=0;c<hypernymsList.size();c++){
PointerTargetNodeList l = hypernymsList.get(c);
for(int j = l.size()-1; j >= 0 ; j--) {
Long tempid = l.get(j).getPointerTarget().getSynset().getOffset();
String tempword = l.get(j).getPointerTarget().getSynset().getWords().get(0).getLemma();
Node n = new Node(tempid, tempword, new ArrayList<Node>());
if (!graph.containsKey(tempid)) {
n.id = tempid;
n.word = tempword;
if (!(j == l.size()-1)){
n.parents.add(graph.get(l.get(j+1).getPointerTarget().getSynset().getOffset()));
}
graph.put(tempid, n);
}
}
}
}
public static void demonstrateListHelper(String text) throws JWNLException {
String lineText =text.split("\t")[2];
String [] singleWord = lineText.split("\\s+");
for (int k=0; k <singleWord.length; k++){
singleWord[k] = singleWord[k].replaceAll("[^\\w]", "");
IndexWordSet set = Dictionary.getDefaultResourceInstance().lookupAllIndexWords(singleWord[k]);
for (IndexWord word:set.getIndexWordArray()) {
demonstrateTree(word);
}
}
}
public static void generateHierarchy() {
Set<Entry<Long, Node>> iterator = graph.entrySet();
int i =0;
for(Entry<Long,Node> e : iterator) {
System.out.println(i++ +" - " +e.getValue().firstParents());
}
}
@SuppressWarnings({ "resource" })
public static void main(String[] args) throws JWNLException {
File file = new File("C:/Users/D060891/Desktop/Thesis/sentencesNYT/part-m-00001");
try {
BufferedReader input = new BufferedReader(new FileReader(file));
String line;
while ((line = input.readLine()) != null) {
demonstrateListHelper(line);
}
generateHierarchy();
}
catch (IOException e) {
e.printStackTrace();
}
}
最佳答案
性能优化的第一条规则是不要盯着代码或猜测,而是要测量运行时行为。因此,启动分析器并查看您的程序将时间(或内存)花在哪里。
一个好的开始是profile你的代码 VisualVM包含在 JDK 中。
更新: 您现在已经确定了瓶颈:
Dictionary.getDefaultResourceInstance()
调查 source code每次调用该方法时,都会从 XML 文档加载一个 wordnet 词典。因此,只需将瓶颈移出循环并在开始时一次获取字典:定义一个类变量
private static Dictionary dictionary;
在开头初始化,例如在主要
dictionary = Dictionary.getDefaultResourceInstance();
以后再用
dictionary.lookupAllIndexWords(singleWord[k]);
关于java - 我怎样才能使这段代码更有效率?循环和大数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36777733/