轻量级前端框架助力开发者提升项目效率与性能
825
2022-10-28
基于JMH对ArrayList和LinkedList插入操作进行性能测试
基于JMH对ArrayList和LinkedList插入操作进行性能测试
Java基准测试工具 —— JMH使用指南
实验思路
测试以下三种情况,插入性能差距。
ArrayListArrayList 指定初始化长度LinkedList
导入maven依赖
测试用代码
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
执行结果
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小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~