当前位置:网站首页>Multithreaded applications - improve efficiency
Multithreaded applications - improve efficiency
2022-06-24 10:32:00 【Sit at a sunny window and drink tea alone】
Environment building
Basic introduction
Benchmarking tool selection , Used a more reliable JMH, It will execute the program , Perform multiple tests and average
cpu Audit limit , There are two ways of thinking
- Using virtual machines , Assign the appropriate core
- Use msconfig, Assign the appropriate core , It's troublesome to restart
Choice of parallel computing methods
Create project
Click on Add , add to groupId : org.openjdk.jmh , Then add ArtifactId : jmh-java-benchmark-archetype


It can also be done through maven Command to create
mvn archetype:generate -DinteractiveMode=false -DarchetypeGroupId=org.openjdk.jmh -
DarchetypeArtifactId=jmh-java-benchmark-archetype
-DgroupId=cn.knightzz
-DartifactId=apply-efficiency
-Dversion=1.0
Remember to go after the creation pom file java Compiled version <javac.target>1.8</javac.target> from 1.6 Cultivation 1.8
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jmh.version>1.0</jmh.version>
<javac.target>1.8</javac.target>
<uberjar.name>benchmarks</uberjar.name>
</properties>
Write test code
package cn.knightzz.benchmark;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
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;
/** * @author Wang Tianci * @title: MyBenchmark * @projectName hm-juc-codes * @description: Test multithreading to improve execution efficiency * @website <a href="http://knightzz.cn/">http://knightzz.cn/</a> * @github <a href="https://github.com/knightzz1998">https://github.com/knightzz1998</a> * @create: 2022-06-19 15:59 */
@Fork(1)
@BenchmarkMode(Mode.AverageTime) // Average time taken for statistical procedures
@Warmup(iterations=3) // Program warm-up times
@Measurement(iterations=5) // Number of tests
@SuppressWarnings("all")
public class MyBenchmark {
static int[] ARRAY = new int[1000_000_00];
static {
// Fill the array with 1
Arrays.fill(ARRAY, 1);
}
@Benchmark
public int c() throws ExecutionException, InterruptedException {
int[] array = ARRAY;
FutureTask<Integer> t1 = new FutureTask<>(() -> {
int sum = 0;
for (int i = 0; i < 250_000_00; i++) {
sum += array[0+i];
}
return sum;
});
FutureTask<Integer> t2 = new FutureTask<>(() -> {
int sum = 0;
for (int i = 0; i < 250_000_00; i++) {
sum += array[250_000_00 + i];
}
return sum;
});
FutureTask<Integer> t3 = new FutureTask<>(() -> {
int sum = 0;
for (int i = 0; i < 250_000_00; i++) {
sum += array[500_000_00 + i];
}
return sum;
});
FutureTask<Integer> 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() {
int[] array = ARRAY;
int sum = 0;
for (int i = 0; i < 1000_000_00; i++) {
sum += array[0 + i];
}
return sum;
}
}
Use maven package become involved jar package

Then the command desk enters target Under the table of contents , perform java -jar -Xmx2G benchmarks.jar
$ java -jar -Xmx2G benchmarks.jar
# VM invoker: C:\dev\Java\jre\bin\java.exe
# VM options: -Xmx2G
# 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: cn.knightzz.benchmark.MyBenchmark.c
# Run progress: 0.00% complete, ETA 00:00:16
# Fork: 1 of 1
# Warmup Iteration 1: 0.019 s/op
# Warmup Iteration 2: 0.020 s/op
# Warmup Iteration 3: 0.019 s/op
Iteration 1: 0.023 s/op
Iteration 2: 0.020 s/op
Iteration 3: 0.020 s/op
Iteration 4: 0.026 s/op
Iteration 5: 0.022 s/op
Result: 0.022 ±(99.9%) 0.009 s/op [Average]
Statistics: (min, avg, max) = (0.020, 0.022, 0.026), stdev = 0.002
Confidence interval (99.9%): [0.013, 0.031]
# VM invoker: C:\dev\Java\jre\bin\java.exe
# VM options: -Xmx2G
# Warmup: 3 iterations, 1 s each
Iteration 4: 0.038 s/op
Iteration 5: 0.039 s/op
Result: 0.039 ±(99.9%) 0.002 s/op [Average]
Statistics: (min, avg, max) = (0.038, 0.039, 0.039), stdev = 0.000
Confidence interval (99.9%): [0.037, 0.041]
# Run complete. Total time: 00:00:20
Benchmark Mode Samples Score Score error Units
c.k.b.MyBenchmark.c avgt 5 0.022 0.009 s/op
c.k.b.MyBenchmark.d avgt 5 0.039 0.002 s/op
The execution result is as shown above : You can see that the efficiency is much lower , Multithreading execution cost 0.022 s , Single thread cost 0.039 s ( The unit is seconds )
边栏推荐
猜你喜欢

希尔排序图文详解+代码实现

线程的 sleep() 方法与 wait() 方法的区别

Machine learning - principal component analysis (PCA)

Role of message queuing

How to customize sharing links in wechat for H5 web pages

Machine learning perceptron and k-nearest neighbor

Status of the thread pool

2022 International Symposium on intelligent robots and systems (isoirs 2022)

Outils de capture de paquets

Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves
随机推荐
希尔排序图文详解+代码实现
6. package management business development
tf.contrib.layers.batch_norm
【IEEE出版】2022年工业自动化,机器人与控制工程国际会议(IARCE 2022)
[resource sharing] 2022 International Conference on Environmental Engineering and Biotechnology (coeeb 2022)
JMeter interface test tool foundation - use badboy to record JMeter script
栈题目:函数的独占时间
How can I solve the problem that the swiper animation animation fails when switching between left and right rotations of the swiper?
JMeter interface test tool foundation - badboy tool
International Symposium on energy and environmental engineering in 2022 (coeee 2022)
leetCode-223: 矩形面积
Sort out interface performance optimization skills and kill slow code
tf. contrib. layers. batch_ norm
Petit guide de construction rapide du bras mécanique (II): application du bras mécanique
88.合并有序数组
Practice sharing of packet capturing tool Charles
2022 the most complete and detailed JMeter interface test tutorial and detailed interface test process in the whole network - JMeter test plan component (thread < user >)
Flink checkPoint和SavePoint
SSM整合
消息队列的作用