问题描述
我在运行英特尔(R)Xeon(R)CPU E5-2680 0 @ 2.70GHz(2个CPU),~2.7GHz
的PC中运行以下示例代码
I ran below sample code in my PC running with Intel(R) Xeon(R) CPU E5-2680 0 @ 2.70GHz (2 CPUs), ~2.7GHz
String format = "%7s run taken %6d micro seconds %5d findAny";
// First run
long start = System.nanoTime();
int rand = IntStream.range(0, 100000).parallel().findAny().getAsInt();
long end = System.nanoTime();
System.out.println(String.format(format, "First", ((end - start) / 1000), rand));
// Subsequent runs
for (int i = 0; i < 25; i++) {
start = System.nanoTime();
rand = IntStream.range(0, 100000).parallel().findAny().getAsInt();
end = System.nanoTime();
System.out.println(String.format(format, "Subseq", ((end - start) / 1000), rand));
}
其输出
First run taken 92532 micro seconds 50000 findAny
Subseq run taken 61 micro seconds 50000 findAny
Subseq run taken 37 micro seconds 50000 findAny
Subseq run taken 52 micro seconds 50000 findAny
Subseq run taken 42 micro seconds 50000 findAny
Subseq run taken 33 micro seconds 50000 findAny
Subseq run taken 32 micro seconds 50000 findAny
Subseq run taken 34 micro seconds 50000 findAny
Subseq run taken 33 micro seconds 50000 findAny
Subseq run taken 34 micro seconds 50000 findAny
Subseq run taken 32 micro seconds 50000 findAny
Subseq run taken 32 micro seconds 50000 findAny
Subseq run taken 46 micro seconds 50000 findAny
Subseq run taken 36 micro seconds 50000 findAny
Subseq run taken 31 micro seconds 50000 findAny
Subseq run taken 43 micro seconds 50000 findAny
Subseq run taken 34 micro seconds 50000 findAny
Subseq run taken 31 micro seconds 50000 findAny
Subseq run taken 32 micro seconds 50000 findAny
Subseq run taken 37 micro seconds 50000 findAny
Subseq run taken 45 micro seconds 50000 findAny
Subseq run taken 49 micro seconds 50000 findAny
Subseq run taken 32 micro seconds 50000 findAny
Subseq run taken 31 micro seconds 50000 findAny
Subseq run taken 31 micro seconds 50000 findAny
Subseq run taken 37 micro seconds 50000 findAny
我们可以看到第一次和后续运行之间的时间差异。
we could see the time taken difference between the first and subsequent runs.
- 是否意味着流操作被缓存?是否为
Java8
中的流实现了内部缓存? - 有时
findAny
返回不同的值,但所花费的时间几乎等于后续运行,而不是像第一次运行那样
- does it mean stream operations are cached? Is there any internal cache implemented for streams in
Java8
? - sometimes
findAny
returns different value but the time taken is almost equal to the subsequent runs not like the first run
见下文
First run taken 84099 micro seconds 50000 findAny
Subseq run taken 163 micro seconds 25000 findAny
Subseq run taken 46 micro seconds 50000 findAny
Subseq run taken 52 micro seconds 25000 findAny
推荐答案
不,为实现lambdas而生成的代码,以及加载的类是缓存的。
No, the code generated to implement the lambdas, and the classes loaded are cached.
Streams没有特殊缓存。
There is no special cache for Streams.
确实。没有关于结果的任何内容被缓存。第一次为加载代码支付罚金。
Indeed. Nothing about the result is cached. The first time you pay a penalty for loading the code.
BTW编码在运行至少10,000次之前并未真正优化。在计时之前,我会反复运行这个测试大约10秒钟。
BTW the coding isn't really optimised until it has been run at least 10,000 times. I would run this test repeatedly for around 10 seconds before timing it.
这篇关于Java8流操作是否缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!