【发布时间】:2014-12-18 12:52:31
【问题描述】:
当我改变缓冲区的大小时,我得到了无法从 BufferedReader 解释的奇怪结果。
我曾强烈预计,随着缓冲区大小的增加,性能会逐渐提高,收益递减会很快出现,此后性能会或多或少持平。但似乎,在只有非常适中的缓冲区大小之后,增加缓冲区的大小会使其变慢。
这是一个最小的例子。它所做的只是遍历一个文本文件,并计算行长的总和。
public int traverseFile(int bufSize) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("words16"), bufSize*1024);
String line;
int total=0;
while ((line=reader.readLine())!=null)
total+=line.length();
reader.close();
return total;
}
我尝试使用各种缓冲区大小对此进行基准测试,结果相当奇怪。最大约256KB,性能提升;在那之后,情况变得更糟。我想知道这是否只是分配缓冲区所花费的时间,所以我尝试添加一些东西以使其始终分配相同的内存总量(见下面的第二行):
public int traverseFile(int bufSize) throws IOException {
byte[] pad = new byte[(65536-bufSize)*1024];
BufferedReader reader = new BufferedReader(new FileReader("words16"), bufSize*1024);
String line;
int total=0;
while ((line=reader.readLine())!=null)
total+=line.length();
reader.close();
return total;
}
这并不难。在两台不同的机器上,我仍然得到相同的结果。以下是完整结果:
Benchmark Mode Samples Score Error Units
j.t.BufferSizeBenchmark.traverse_test1_4K avgt 100 363.987 ± 1.901 ms/op
j.t.BufferSizeBenchmark.traverse_test2_16K avgt 100 356.551 ± 0.330 ms/op
j.t.BufferSizeBenchmark.traverse_test3_64K avgt 100 353.462 ± 0.557 ms/op
j.t.BufferSizeBenchmark.traverse_test4_256K avgt 100 350.822 ± 0.562 ms/op
j.t.BufferSizeBenchmark.traverse_test5_1024K avgt 100 356.949 ± 0.338 ms/op
j.t.BufferSizeBenchmark.traverse_test6_4096K avgt 100 358.377 ± 0.388 ms/op
j.t.BufferSizeBenchmark.traverse_test7_16384K avgt 100 367.890 ± 0.393 ms/op
j.t.BufferSizeBenchmark.traverse_test8_65536K avgt 100 363.271 ± 0.228 ms/op
如您所见,最佳位置约为 256KB。差别不大,但肯定是可以衡量的。
我能想到的只是这可能与内存缓存有关。是因为正在写入的 RAM 离正在读取的 RAM 更远吗?但如果它是一个循环缓冲区,我什至不确定这是不是真的:正在写入的内容将位于正在读取的内容之后。
words16 文件是 80MB,所以我不能在这里发布,但它是 Fedora 的标准 /usr/share/dict/words 文件,超过了 16 倍。如有必要,我可以找到发布链接的方法。
这是基准测试代码:
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@OperationsPerInvocation(1)
@Warmup(iterations = 30, time = 100, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 100, time = 10000, timeUnit = TimeUnit.MILLISECONDS)
@State(Scope.Thread)
@Threads(1)
@Fork(1)
public class BufferSizeBenchmark {
public int traverseFile(int bufSize) throws IOException {
byte[] pad = new byte[(65536-bufSize)*1024];
BufferedReader reader = new BufferedReader(new FileReader("words16"), bufSize*1024);
String line;
int total=0;
while ((line=reader.readLine())!=null)
total+=line.length();
reader.close();
return total;
}
@Benchmark
public int traverse_test1_4K() throws IOException {
return traverseFile(4);
}
@Benchmark
public int traverse_test2_16K() throws IOException {
return traverseFile(16);
}
@Benchmark
public int traverse_test3_64K() throws IOException {
return traverseFile(64);
}
@Benchmark
public int traverse_test4_256K() throws IOException {
return traverseFile(256);
}
@Benchmark
public int traverse_test5_1024K() throws IOException {
return traverseFile(1024);
}
@Benchmark
public int traverse_test6_4096K() throws IOException {
return traverseFile(4096);
}
@Benchmark
public int traverse_test7_16384K() throws IOException {
return traverseFile(16384);
}
@Benchmark
public int traverse_test8_65536K() throws IOException {
return traverseFile(65536);
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(
".*" + BufferSizeBenchmark.class.getSimpleName() + ".*")
.forks(1).build();
new Runner(opt).run();
}
}
为什么当我增加缓冲区大小时性能会变差?
【问题讨论】:
标签: java performance file-io buffer bufferedreader