是否有任何负面的创造巨大的字符串?例如,如果我们从一个潜在的巨大的文本文件中读取文本:
while (scanner.hasNext()) { someString += scanner.next(); } // do something cool with someString
谢谢
解决方法
流媒体vs不
当您可以流式传输时,您可以处理任何大小的文件(假设您真的可以忘记所有已经看到的数据).你最终会有一个自然的O(n)复杂性,这是一件非常好的事情.你不会因为内存不足而中断.
流式可爱…但在每种情况下都不起作用.
StringBuilder的
似乎对StringBuilder建议有一定的争议,这里是显示效果的基准.我不得不减小基准的大小,以便在合理的时间内慢速版本甚至完成.
结果先,然后代码.这是一个非常粗糙和准备的基准,但结果足够戏剧性,使得点…
c:\Users\Jon\Test>java Test slow Building a string of length 120000 without StringBuilder took 21763ms c:\Users\Jon\Test>java Test fast Building a string of length 120000 with StringBuilder took 7ms
和代码…
class FakeScanner { private int linesLeft; private final String line; public FakeScanner(String line,int count) { linesLeft = count; this.line = line; } public boolean hasNext() { return linesLeft > 0; } public String next() { linesLeft--; return line; } } public class Test { public static void main(String[] args) { FakeScanner scanner = new FakeScanner("test",30000); boolean useStringBuilder = "fast".equals(args[0]); // Accurate enough for this test long start = System.currentTimeMillis(); String someString; if (useStringBuilder) { StringBuilder builder = new StringBuilder(); while (scanner.hasNext()) { builder.append(scanner.next()); } someString = builder.toString(); } else { someString = ""; while (scanner.hasNext()) { someString += scanner.next(); } } long end = System.currentTimeMillis(); System.out.println("Building a string of length " + someString.length() + (useStringBuilder ? " with" : " without") + " StringBuilder took " + (end - start) + "ms"); } }