這是我的功能:
public void addToList() throws IOException {
String urlString = "http://web.stanford.edu/class/archive/cs/cs106l/cs106l.1102/assignments/dictionary.txt";
URL url = new URL(urlString);
Scanner scannerWords = new Scanner(url.openStream());
while (scannerWords.hasNextLine()) {
words.add(scannerWords.nextLine());
}
}
這需要: 32.8 秒的運行時間才能執行。
無論如何我可以優化它(也許每 10 行讀取一次)?
uj5u.com熱心網友回復:
這是我的嘗試。我沒有使用掃描儀,而是逐個字符地閱讀。這減少了使用 Scanner 的開銷和層數。
String urlString = "http://web.stanford.edu/class/archive/cs/cs106l/cs106l.1102/assignments/dictionary.txt";
InputStream stream = new URL(urlString).openStream();
BufferedInputStream bufferedStream = new BufferedInputStream(stream);
ArrayList<String> words = new ArrayList<>();
char[] chars = new char[100];
int index = 0;
long currentTimeMillis = System.currentTimeMillis();
while(true){
int c = bufferedStream.read();
if (c == '\n'){
words.add(new String(chars, 0, index));
index=0;
} else if (c < 0){
words.add(new String(chars, 0, index));
break;
} else {
chars[index ] = (char) c;
}
}
long currentTimeMillis1 = System.currentTimeMillis();
stream.close();
System.out.println("Time = " (currentTimeMillis1-currentTimeMillis) " ms");
System.out.println("Word count = " words.size());
System.out.println( "First word = " words.get(0));
System.out.println( "Last word = " words.get(words.size()-1));
}
輸出
run:
Time = 707 ms
Word count = 127142
First word = aa
Last word = zyzzyvas
BUILD SUCCESSFUL (total time: 0 seconds)
uj5u.com熱心網友回復:
嗯,顯而易見的事情是只需下載一次單詞串列并使用本地副本,而不是每次運行程式時都通過網路獲取它。
您有泄漏,因為您從未關閉回傳的流URL.openStream()(如果您將其更改為使用檔案,則當前代碼將存在相同的問題)。通過scannerWords.close();在回圈后添加 a 很容易解決這個問題,但更好的例外安全方法是使用try-with-resources。
不過,我會Scanner完全省去 ,而只使用BufferedReader。就像是:
import java.net.URL;
import java.util.*;
import java.util.stream.*;
import java.io.*;
// ...
private List<String> readLinesFromURL(String url) throws IOException {
try (BufferedReader br
= new BufferedReader(new InputStreamReader(new URL(url).openStream()))) {
return br
.lines()
.collect(Collectors.toCollection(ArrayList<String>::new));
}
}
uj5u.com熱心網友回復:
- 一起獲取所有資料
- 應用您的過濾器以獲得預期的詞
public static void main(String[] args) throws IOException {
printWords(new ArrayList<>(150000));
}
private static void printWords(List<String> list) throws IOException {
final long l = System.currentTimeMillis();
String urlString = "http://web.stanford.edu/class/archive/cs/cs106l/cs106l.1102/assignments/dictionary.txt";
URL url = new URL(urlString);
final long l2;
final long l3;
Charset encoding=Charset.defaultCharset();
try (Scanner scanner = new Scanner(url.openStream(), String.valueOf(encoding))) {
l2 = System.currentTimeMillis();
String content = scanner.useDelimiter("\\A").next();
list = Arrays.asList(content.split("\\n"));
l3 = System.currentTimeMillis();
//System.out.println(list);
}
final long l4 = System.currentTimeMillis();
System.out.println(String.format("Total Time: %d",l4-l));
System.out.println(String.format("Data fetching Time: %d",l2-l));
System.out.println(String.format("Data collection Time: %d",l3-l2));
}
輸出:
Total Time: 2482
Data fetching Time: 465
Data collection Time: 2017
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/387126.html
