当前位置:网站首页>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 )
边栏推荐
- [ei sharing] the 6th International Conference on ship, ocean and Maritime Engineering in 2022 (naome 2022)
- 学习使用phpstripslashe函数去除反斜杠
- Learning to organize using kindeditor rich text editor in PHP
- 【IEEE出版】2022年智能交通与未来出行国际会议(CSTFM 2022)
- leetCode-面试题 01.05: 一次编辑
- leetCode-面试题 16.06: 最小差
- 线程运行原理
- 【IEEE出版】2022年工业自动化,机器人与控制工程国际会议(IARCE 2022)
- 3. addition, deletion, modification and query of employees
- Flink cluster construction and enterprise level yarn cluster construction
猜你喜欢

进程与多线程

SQL Server AVG function rounding

Leetcode-498: diagonal traversal

Machine learning perceptron and k-nearest neighbor

5.菜品管理业务开发

Customize the toolbars of the kindeditor editor. Items removes unnecessary toolbars or retains some toolbars

栈题目:函数的独占时间

How can I solve the problem that the swiper animation animation fails when switching between left and right rotations of the swiper?

Normal equation

百度网盘下载一直请求中问题解决
随机推荐
JMeter interface test tool foundation - sampler (II)
Leetcode-2221: triangular sum of arrays
Baidu online disk download has been in the process of requesting solutions
整理接口性能优化技巧,干掉慢代码
Leetcode-1089: replication zero
numpy. linspace()
International Symposium on energy and environmental engineering in 2022 (coeee 2022)
Illustration miscellaneous [for archiving to prevent loss]
线程池的执行流程
2022 International Symposium on intelligent robots and systems (isoirs 2022)
A method to solve the self-adaptive width and height of the internal picture of rich text label in wechat applet
顺丰科技智慧物流校园技术挑战赛(2022/06/19)【AK】
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 >)
抓包工具charles实践分享
解决Deprecated: Methods with the same name as their class will not be constructors in报错方案
SQL Server AVG function rounding
Solve the timeout of Phoenix query of dbeaver SQL client connection
Image click enlargement and adaptive size in the applet rich text
SSM integration
4. classification management business development