我知道这里和网上有几个类似的线程,但我想我似乎做错了什么。我的任务很简单 - 将一个大整数数组(int [] 或 ArrayList 或您认为最好的)写入(然后读取)到文件中。越快越好。我的具体数组中有大约 450 万个整数,当前时间例如(以毫秒为单位):
- 生成树:14851.13071
- 生成数组:2237.4661619999997
- 保存数组:89250.167617
- 加载数组:114908.08185799999
这是不可接受的,我想时间应该少得多。我究竟做错了什么?我不需要地球上最快的方法,但将这些时间缩短到大约 5 - 15 秒(欢迎减少但不是强制性的)是我的目标。
我当前的代码:
long start = System.nanoTime();
Node trie = dawg.generateTrie("dict.txt");
long afterGeneratingTrie = System.nanoTime();
ArrayList<Integer> array = dawg.generateArray(trie);
long afterGeneratingArray = System.nanoTime();
try
{
new ObjectOutputStream(new FileOutputStream("test.txt")).writeObject(array);
}
catch (Exception e)
{
Logger.getLogger(DawgTester.class.getName()).log(Level.SEVERE, null, e);
}
long afterSavingArray = System.nanoTime();
ArrayList<Integer> read = new ArrayList<Integer>();
try
{
read = (ArrayList)new ObjectInputStream(new FileInputStream("test.txt")).readObject();
}
catch (Exception e)
{
Logger.getLogger(DawgTester.class.getName()).log(Level.SEVERE, null, e);
}
long afterLoadingArray = System.nanoTime();
System.out.println("Generating trie: " + 0.000001 * (afterGeneratingTrie - start));
System.out.println("Generating array: " + 0.000001 * (afterGeneratingArray - afterGeneratingTrie));
System.out.println("Saving array: " + 0.000001 * (afterSavingArray - afterGeneratingArray));
System.out.println("Loading array: " + 0.000001 * (afterLoadingArray - afterSavingArray));