使用JMH 1.20,我有以下基准测试类来测量求和整数的性能差异:
IntStream
IntStream
parallel 这是java类:
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.stream.IntStream;
@Fork(1)
@Warmup(iterations = 10)
@Measurement(iterations = 10)
@State(Scope.Benchmark)
public class SumBenchmark {
@Param({"10000", "100000", "1000000", "10000000"})
private int n;
@Benchmark
public int sumForLoop() {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += i;
}
return sum;
}
@Benchmark
public int sumIntStream() {
return IntStream.range(0, n).sum();
}
@Benchmark
public int sumIntStreamParallel() {
return IntStream.range(0, n).parallel().sum();
}
public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder()
.include(SumBenchmark.class.getName())
.build();
new Runner(options).run();
}
}
这是在JDK 1.8.0_131,macOS 10.13.3,2.4 GHz Intel Core 2 Duo上运行后的结果(重新安排了可读性):
Benchmark (n) Mode Cnt Score Error Units
SumBenchmark.sumForLoop 10000 thrpt 10 160635.169 ± 9716.597 ops/s
SumBenchmark.sumIntStream 10000 thrpt 10 23270.515 ± 388.520 ops/s
SumBenchmark.sumIntStreamParallel 10000 thrpt 10 64018.729 ± 3424.532 ops/s
SumBenchmark.sumForLoop 100000 thrpt 10 17314.287 ± 238.582 ops/s
SumBenchmark.sumIntStream 100000 thrpt 10 1035.085 ± 16.062 ops/s
SumBenchmark.sumIntStreamParallel 100000 thrpt 10 24411.996 ± 255.924 ops/s
SumBenchmark.sumForLoop 1000000 thrpt 10 1846.943 ± 60.891 ops/s
SumBenchmark.sumIntStream 1000000 thrpt 10 104.376 ± 3.396 ops/s
SumBenchmark.sumIntStreamParallel 1000000 thrpt 10 323.331 ± 691.844 ops/s
SumBenchmark.sumForLoop 10000000 thrpt 10 184.656 ± 6.381 ops/s
SumBenchmark.sumIntStream 10000000 thrpt 10 189.411 ± 3.718 ops/s
SumBenchmark.sumIntStreamParallel 10000000 thrpt 10 18.006 ± 0.503 ops/s
我对此有一些疑问:
sumForLoop
为什么sumIntStream
比n <= 1000000
快sumIntStreamParallel
?n == 10000000
时,为什么 @FXML
private Button btnLogOut;
@FXML
private Label lblStatus;
@FXML
private void btnLogOut_Click() throws InterruptedException {
lblStatus.setText("Logging out.."); // Doesn't work..?
Thread.sleep(1000);
System.exit(0);
}
这么慢?感谢。