我的程序在while循环中逐行读取文本文件.然后它处理每一行并提取要在输出中写入的一些信息.它在while循环中所做的一切都是O(1),除了两个我认为是O(N)的ArrayList indexOf()方法调用.该程序在开始时以合理的速度运行(每100秒1M行),但随着时间的推移它会大幅减速.我在输入文件中有70 M行,因此循环迭代7000万次.从理论上讲,这应该需要大约2个小时,但实际上需要13个小时.问题出在哪儿?
这是代码片段:
BufferedReader corpus = new BufferedReader(
new InputStreamReader(
new FileInputStream("MyCorpus.txt"),"UTF8"));
Writer outputFile = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("output.txt"), "UTF-8"));
List words = new ArrayList();
//words is being updated with relevant values here
LinkedHashMap DIC = new LinkedHashMap();
//DIC is being updated with relevant key-value pairs here
String line = "";
while ((line = corpus.readLine()) != null)
String[] parts = line.split(" ");
if (DIC.containsKey(parts[0]