应用篇效率1.使用多线程充分利用 CPU1)环境搭建基准测试工具选择,使用了比较靠谱的 JMH,它会执行程序预热,执行多次测试并平均cpu 核数限制,有两种思路1.使用虚拟机,分配合适的核2.使用 mscon?g,分配合适的核,需要重启比较麻烦并行计算方式的选择1.最初想直接使用 parallel stream,后来发现它有自己的问题2.改为了自己手动控制 thread,实现简单的并行计算测试代码如下 mvn archetype:generate -DinteractiveMode=false -DarchetypeGroupId=org.openjdk.jmh -DarchetypeArtifactId=jmh-java-benchmark-archetype -DgroupId=org.sample -DartifactId=test -Dversion=1.0 package org.sample;import java.util.Arrays;import java.util.concurrent.FutureTask;import org.openjdk.jmh.annotations.Benchmark;import org.openjdk.jmh.annotations.BenchmarkMode;import org.openjdk.jmh.annotations.Fork;import org.openjdk.jmh.annotations.Measurement;import org.openjdk.jmh.annotations.Mode;import org.openjdk.jmh.annotations.Warmup;@Fork(1)@BenchmarkMode(Mode.AverageTime)@Warmup(iterations=3)@Measurement(iterations=5)public class MyBenchmark { static int[] ARRAY = new int[1000_000_00]; static { Arrays.fill(ARRAY, 1); } @Benchmark public int c() throws Exception {int[] array = ARRAY; FutureTask t1 = new FutureTask<>(()->{ int sum = 0; for(int i = 0; i < 250_000_00;i++) { sum += array[0+i]; } return sum; }); FutureTask t2 = new FutureTask<>(()->{ int sum = 0; for(int i = 0; i < 250_000_00;i++) { sum += array[250_000_00+i]; } return sum; }); FutureTask t3 = new FutureTask<>(()->{ int sum = 0; for(int i = 0; i < 250_000_00;i++) { sum += array[500_000_00+i]; } return sum; }); FutureTask t4 = new FutureTask<>(()->{ int sum = 0; for(int i = 0; i < 250_000_00;i++) { sum += array[750_000_00+i]; } return sum; }); new Thread(t1).start(); new Thread(t2).start(); new Thread(t3).start(); new Thread(t4).start(); return t1.get() + t2.get() + t3.get()+ t4.get(); } @Benchmark public int d() throws Exception { int[] array = ARRAY; FutureTask t1 = new FutureTask<>(()->{ int sum = 0; for(int i = 0; i < 1000_000_00;i++) { sum += array[0+i]; } return sum; }); new Thread(t1).start(); return t1.get(); }}?2) 双核 CPU(4个逻辑CPU) C:\\Users\\lenovo\\eclipse-workspace\\test>java -jar target/benchmarks.jar# VM invoker: C:\\Program Files\\Java\\jdk-11\\bin\\java.exe# VM options: # Warmup: 3 iterations, 1 s each# Measurement: 5 iterations, 1 s each# Threads: 1 thread, will synchronize iterations# Benchmark mode: Average time, time/op# Benchmark: org.sample.MyBenchmark.c # Run progress: 0.00% complete, ETA 00:00:16# Fork: 1 of 1# Warmup Iteration 1: 0.022 s/op# Warmup Iteration 2: 0.019 s/op# Warmup Iteration 3: 0.020 s/opIteration 1: 0.020 s/opIteration 2: 0.020 s/opIteration 3: 0.020 s/opIteration 4: 0.020 s/opIteration 5: 0.020 s/op Result: 0.020 ±(99.9%) 0.001 s/op [Average] Statistics: (min, avg, max) = (0.020, 0.020, 0.020), stdev = 0.000 Confidence interval (99.9%): [0.019, 0.021] # VM invoker: C:\\Program Files\\Java\\jdk-11\\bin\\java.exe# VM options: # Warmup: 3 iterations, 1 s each# Measurement: 5 iterations, 1 s each# Threads: 1 thread, will synchronize iterations# Benchmark mode: Average time, time/op# Benchmark: org.sample.MyBenchmark.d # Run progress: 50.00% complete, ETA 00:00:10# Fork: 1 of 1# Warmup Iteration 1: 0.042 s/op# Warmup Iteration 2: 0.042 s/op# Warmup Iteration 3: 0.041 s/opIteration 1: 0.043 s/opIteration 2: 0.042 s/opIteration 3: 0.042 s/opIteration 4: 0.044 s/opIteration 5: 0.042 s/op Result: 0.043 ±(99.9%) 0.003 s/op [Average] Statistics: (min, avg, max) = (0.042, 0.043, 0.044), stdev = 0.001 Confidence interval (99.9%): [0.040, 0.045] # Run complete. Total time: 00:00:20