基于JMH对ArrayList和LinkedList插入操作进行性能测试

网友投稿 825 2022-10-28

基于JMH对ArrayList和LinkedList插入操作进行性能测试

基于JMH对ArrayList和LinkedList插入操作进行性能测试

基于JMH对ArrayList和LinkedList插入操作进行性能测试

​​Java基准测试工具 —— JMH使用指南​​

实验思路

测试以下三种情况,插入性能差距。

ArrayListArrayList 指定初始化长度LinkedList

导入maven依赖

org.openjdk.jmh jmh-core 1.23 org.openjdk.jmh jmh-generator-annprocess 1.23

测试用代码

import org.openjdk.jmh.annotations.*;import org.openjdk.jmh.infra.Blackhole;import org.openjdk.jmh.runner.Runner;import org.openjdk.jmh.runner.RunnerException;import org.openjdk.jmh.runner.options.Options;import org.openjdk.jmh.runner.options.OptionsBuilder;import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.concurrent.TimeUnit;@BenchmarkMode(Mode.AverageTime)@Warmup(iterations = 3, time = 1)@Measurement(iterations = 5, time = 5)@Threads(4)@Fork(1)@State(value = Scope.Benchmark)@OutputTimeUnit(TimeUnit.NANOSECONDS)public class ListTest { private int size = 500_0000; public static void main(String[] args) throws RunnerException { Options opt = new OptionsBuilder() .include(ListTest.class.getSimpleName()) .forks(1) .build(); new Runner(opt).run(); } @Benchmark public void arrayList(Blackhole blackhole) { List a = new ArrayList<>(); for (int i = 0; i < size; i++) { a.add(1); } System.out.println(a.size()); } @Benchmark public void arrayList2(Blackhole blackhole) { List a = new ArrayList<>(size); for (int i = 0; i < size; i++) { a.add(1); } System.out.println(a.size()); } @Benchmark public void linkedList(Blackhole blackhole) { List a = new LinkedList<>(); for (int i = 0; i < size; i++) { a.add(1); } System.out.println(a.size()); }}

执行结果

REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up onwhy the numbers are the way they are. Use profilers (see -prof, -lprof), design factorialexperiments, perform baseline and negative tests that provide experimental control, make surethe benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.Do not assume the numbers tell you what you want them to tell.Benchmark Mode Cnt Score Error UnitsListTest.arrayList avgt 5 90917367.510 ± 18681506.307 ns/opListTest.arrayList2 avgt 5 37638850.686 ± 13428284.589 ns/opListTest.linkedList avgt 5 337937895.252 ± 308730540.843

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:vue+webpack 多页应用框架【可集成单页应用】
下一篇:关于使用jpa聚合函数遇到的问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~