Created
May 2, 2022 06:02
-
-
Save lujiajing1126/17c5372af0e940a21067a3e6f8554594 to your computer and use it in GitHub Desktop.
Load factor banchmark for HashMap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import org.openjdk.jmh.annotations.*; | |
import org.openjdk.jmh.infra.Blackhole; | |
import org.openjdk.jmh.profile.GCProfiler; | |
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.HashMap; | |
import java.util.Map; | |
import java.util.concurrent.TimeUnit; | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) | |
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) | |
@State(Scope.Benchmark) | |
public class HashMapLoadFactorBenchmark { | |
@Param({"1", "2", "3", "4", "5", "10", "15", "20"}) | |
public int arg; | |
@Benchmark | |
public void loadFactorDefault(Blackhole bh) { | |
final Map<String, String> map = new HashMap<>(arg); | |
for (int i = 0; i < arg; i++) { | |
map.put("key" + i, "value"); | |
} | |
bh.consume(map); | |
} | |
@Benchmark | |
public void loadFactorOne(Blackhole bh) { | |
final Map<String, String> map = new HashMap<>(arg, 1); | |
for (int i = 0; i < arg; i++) { | |
map.put("key" + i, "value"); | |
} | |
bh.consume(map); | |
} | |
public static void main(String[] args) throws RunnerException { | |
Options opt = new OptionsBuilder() | |
.include(".*" + HashMapLoadFactorBenchmark.class.getSimpleName() + ".*") | |
.addProfiler(GCProfiler.class) | |
.build(); | |
new Runner(opt).run(); | |
} | |
} |
Author
lujiajing1126
commented
May 2, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment