gpt4 book ai didi

java - 为什么在我的 BufferedReader 中使用*更大*的缓冲区时性能会*差*?

转载 作者:行者123 更新时间:2023-12-03 11:01:05 25 4
gpt4 key购买 nike

当我改变缓冲区的大小时,我得到了无法从 BufferedReader 解释的奇怪结果。

我曾强烈期望性能会随着缓冲区大小的增加而逐渐增加, yield 递减设置相当快,此后性能或多或少会持平。但看起来,在只有非常适中的缓冲区大小之后,增加缓冲区大小会使其变慢

这是一个最小的例子。它所做的只是遍历一个文本文件,并计算行的长度总和。

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();
}

}

为什么当我增加缓冲区大小时性能会变差?

最佳答案

这很可能是缓存行大小的影响。由于缓存使用 LRU 逐出策略,使用太大的缓冲区会导致您写入缓冲区“开始”的内容在您有机会读取它之前被逐出。

关于java - 为什么在我的 BufferedReader 中使用*更大*的缓冲区时性能会*差*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26518185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com