谁能解释为什么会出现这种情况?文件大小高达2MB。代码执行时间少于2秒。较大差异读
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
catch(Exception e)
{
}
但是,当我的代码更改为:
String temp = "";
try {
while ((line = br.readLine()) != null) {
temp =temp + line;
}
catch(Exception e)
{
}
我明白这将需要相对更长的时间,但它需要470秒,大量的时间。为什么这个区别?
http://stackoverflow.com/questions/1126388/slow-string-concatenation-over-large-input – assylias
字符串连接速度很慢。 – Haile
'StringBuilder'可能是一个选项! – NINCOMPOOP