当前位置:网站首页>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 )
边栏推荐
- SQL Server AVG function rounding
- 5. dish management business development
- 正规方程、、、
- uniapp实现点击拨打电话功能
- leetCode-223: 矩形面积
- 2022年智能机器人与系统国际研讨会(ISoIRS 2022)
- The difference between static link library and dynamic link library
- SSM integration
- Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves
- 解决DBeaver SQL Client 连接phoenix查询超时
猜你喜欢

机械臂速成小指南(三):机械臂的机械结构

Leetcode-223: rectangular area

uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。

JMeter接口测试工具基础— 使用Badboy录制JMeter脚本

JMeter interface test tool foundation - sampler (II)

Solve the timeout of Phoenix query of dbeaver SQL client connection

How to customize sharing links in wechat for H5 web pages

Flink checkpoint and savepoint

【Energy Reports期刊发表】2022年能源与环境工程国际会议(CFEEE 2022)

Baidu online disk download has been in the process of requesting solutions
随机推荐
JMeter接口测试工具基础 — Badboy工具
Common methods of thread scheduling
[IEEE publication] 2022 International Conference on industrial automation, robotics and Control Engineering (iarce 2022)
2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
numpy. linspace()
解决DBeaver SQL Client 连接phoenix查询超时
charles抓包工具使用教程
JMeter interface test tool foundation - sampler (II)
web网站开发,图片懒加载
Spark提交参数--files的使用
SSM整合
JMeter interface test tool foundation - badboy tool
How to use multiple kindeditor editors on a page and pass values to the server
2. login and exit function development
消息队列的作用
機械臂速成小指南(二):機械臂的應用
机械臂速成小指南(三):机械臂的机械结构
Leetcode - 498: traversée diagonale
Niuke-top101-bm29
Caching mechanism for wrapper types