Last active
April 27, 2023 10:16
-
-
Save billybong/f4abdc9589f7531f14079dca05fa6f37 to your computer and use it in GitHub Desktop.
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 net.mojang.benchmark; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.infra.Blackhole; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.options.Options; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
import org.openjdk.jmh.runner.options.TimeValue; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.concurrent.Thread; | |
public class MapIteration { | |
/** | |
* Benchmark Mode Cnt Score Error Units | |
* MapIteration.enhancedFor thrpt 3 26560.045 ± 1348.768 ops/ms | |
* MapIteration.lambda thrpt 3 44321.144 ± 14483.751 ops/ms | |
*/ | |
@State(Scope.Benchmark) | |
public static class BenchmarkState { | |
final Map<String, String> map = new HashMap<>(); | |
@Setup | |
public void setup() { | |
map.put("1", "one"); | |
map.put("2", "two"); | |
map.put("3", "three"); | |
} | |
} | |
@Benchmark | |
public void lambda(final Blackhole blackhole, final BenchmarkState state) { | |
state.map.forEach((key, value) -> { | |
blackhole.consume(key); | |
blackhole.consume(value); | |
}); | |
} | |
@Benchmark | |
public void enhancedFor(final Blackhole blackhole, final BenchmarkState state) { | |
for (final Map.Entry<String, String> entry : state.map.entrySet()) { | |
blackhole.consume(entry.getKey()); | |
blackhole.consume(entry.getValue()); | |
} | |
} | |
public static void main(final String[] args) throws Exception { | |
final Options opt = new OptionsBuilder() | |
.include(MapIteration.class.getName() + ".*") | |
.mode(Mode.Throughput) | |
.timeUnit(TimeUnit.MILLISECONDS) | |
.warmupTime(TimeValue.seconds(20)) | |
.warmupIterations(3) | |
.measurementTime(TimeValue.seconds(20)) | |
.measurementIterations(3) | |
.threads(1) | |
.forks(1) | |
.detectJvmArgs() | |
.shouldFailOnError(true) | |
.shouldDoGC(true) | |
.build(); | |
new Runner(opt).run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment