我是 Java 8 的新手。我仍然不深入了解 API,但我做了一个小型的非正式基准测试来比较新 Streams API 与旧的 Collections 的性能。
测试包括过滤Integer
列表,并为每个偶数计算平方根并将其存储在Double
的结果List
中。
这是代码:
public static void main(String[] args) {
//Calculating square root of even numbers from 1 to N
int min = 1;
int max = 1000000;
List<Integer> sourceList = new ArrayList<>();
for (int i = min; i < max; i++) {
sourceList.add(i);
}
List<Double> result = new LinkedList<>();
//Collections approach
long t0 = System.nanoTime();
long elapsed = 0;
for (Integer i : sourceList) {
if(i % 2 == 0){
result.add(Math.sqrt(i));
}
}
elapsed = System.nanoTime() - t0;
System.out.printf("Collections: Elapsed time:\t %d ns \t(%f seconds)%n", elapsed, elapsed / Math.pow(10, 9));
//Stream approach
Stream<Integer> stream = sourceList.stream();
t0 = System.nanoTime();
result = stream.filter(i -> i%2 == 0).map(i -> Math.sqrt(i)).collect(Collectors.toList());
elapsed = System.nanoTime() - t0;
System.out.printf("Streams: Elapsed time:\t\t %d ns \t(%f seconds)%n", elapsed, elapsed / Math.pow(10, 9));
//Parallel stream approach
stream = sourceList.stream().parallel();
t0 = System.nanoTime();
result = stream.filter(i -> i%2 == 0).map(i -> Math.sqrt(i)).collect(Collectors.toList());
elapsed = System.nanoTime() - t0;
System.out.printf("Parallel streams: Elapsed time:\t %d ns \t(%f seconds)%n", elapsed, elapsed / Math.pow(10, 9));
}.
以下是双核机器的结果:
Collections: Elapsed time: 94338247 ns (0,094338 seconds)
Streams: Elapsed time: 201112924 ns (0,201113 seconds)
Parallel streams: Elapsed time: 357243629 ns (0,357244 seconds)
对于这个特定的测试,流的速度大约是集合的两倍,并且并行性没有帮助(或者我使用错误的方式?)。
问题:
- 这个测试公平吗?我有什么错误吗?
- 流比集合慢吗?有人对此制定了良好的正式基准吗?
- 我应该努力采用哪种方法?
更新结果。
按照 @pveentjer 的建议,我在 JVM 预热(1k 次迭代)后运行了 1k 次测试:
Collections: Average time: 206884437,000000 ns (0,206884 seconds)
Streams: Average time: 98366725,000000 ns (0,098367 seconds)
Parallel streams: Average time: 167703705,000000 ns (0,167704 seconds)
在这种情况下,流的性能更高。我想知道在运行时仅调用一次或两次过滤函数的应用程序中会观察到什么。
最佳答案
除了使用迭代器从列表中间进行大量删除之外,停止使用
LinkedList
进行任何操作。停止手动编写基准测试代码,使用 JMH .
正确的基准:
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@BenchmarkMode(Mode.AverageTime)
@OperationsPerInvocation(StreamVsVanilla.N)
public class StreamVsVanilla {
public static final int N = 10000;
static List<Integer> sourceList = new ArrayList<>();
static {
for (int i = 0; i < N; i++) {
sourceList.add(i);
}
}
@Benchmark
public List<Double> vanilla() {
List<Double> result = new ArrayList<>(sourceList.size() / 2 + 1);
for (Integer i : sourceList) {
if (i % 2 == 0){
result.add(Math.sqrt(i));
}
}
return result;
}
@Benchmark
public List<Double> stream() {
return sourceList.stream()
.filter(i -> i % 2 == 0)
.map(Math::sqrt)
.collect(Collectors.toCollection(
() -> new ArrayList<>(sourceList.size() / 2 + 1)));
}
}
结果:
Benchmark Mode Samples Mean Mean error Units
StreamVsVanilla.stream avgt 10 17.588 0.230 ns/op
StreamVsVanilla.vanilla avgt 10 10.796 0.063 ns/op
正如我预期的那样,流实现速度相当慢。 JIT 能够内联所有 lambda 内容,但不会生成像普通版本那样完美简洁的代码。
一般来说,Java 8 流并不神奇。它们无法加速已经实现良好的事物(可能使用普通迭代或用 Iterable.forEach()
和 Collection.removeIf()
调用替换 Java 5 的 for-each 语句)。流更多的是关于编码的便利性和安全性。方便——速度权衡在这里发挥作用。
关于Java 8 : performance of Streams vs Collections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34060466/