Created
April 30, 2015 12:55
-
-
Save bmorris591/0e0f3859cc8b7e0ab84b to your computer and use it in GitHub Desktop.
JMH Benchmark of `enum` iterations techniques
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
public class App { | |
public static void main(final String[] args) throws Exception { | |
Options opts = new OptionsBuilder() | |
.include(".*") | |
.warmupIterations(10) | |
.measurementIterations(10) | |
.forks(10) | |
.resultFormat(ResultFormatType.TEXT) | |
.build(); | |
new Runner(opts).run(); | |
} | |
@Benchmark | |
public void loopOverValues(final Blackhole b) { | |
for (final Test t : Test.values()) { | |
b.consume(t); | |
} | |
} | |
@Benchmark | |
public void loopOverEnumSet(final Blackhole b) { | |
for (final Test t : EnumSet.allOf(Test.class)) { | |
b.consume(t); | |
} | |
} | |
@Benchmark | |
public void streamValues(final Blackhole b) { | |
Arrays.stream(Test.values()).forEach(b::consume); | |
} | |
@Benchmark | |
public void streamEnumSet(final Blackhole b) { | |
EnumSet.allOf(Test.class).forEach(b::consume); | |
} | |
@Benchmark | |
public void loopCachedEnumSet(final Blackhole b) { | |
for (final Test t : Test.valueSet()) { | |
b.consume(t); | |
} | |
} | |
@Benchmark | |
public void streamCachedEnumSet(final Blackhole b) { | |
Test.valueSet().forEach(b::consume); | |
} | |
@Benchmark | |
public void loopImmutableCachedEnumSet(final Blackhole b) { | |
for (final Test t : Test.immValueSet()) { | |
b.consume(t); | |
} | |
} | |
@Benchmark | |
public void streamImmutableCachedEnumSet(final Blackhole b) { | |
Test.immValueSet().forEach(b::consume); | |
} | |
enum Test { | |
ONE, | |
TWO, | |
THREE, | |
FOUR, | |
FIVE, | |
SIX, | |
SEVEN, | |
EIGHT, | |
NINE, | |
TEN, | |
EVEVEN, | |
TWELVE, | |
THIRTEEN, | |
FOURTEEN, | |
FIFTEEN, | |
SIXTEEN, | |
SEVENTEEN, | |
EIGHTEEN, | |
NINETEEN, | |
TWENTY, | |
TWENTY_ONE, | |
TWENTY_TWO, | |
TWENTY_THREE, | |
TWENTY_FOUR, | |
TWENTY_FIVE, | |
TWENTY_SIX, | |
TWENTY_SEVEN, | |
TWENTY_EIGHT, | |
TWENTY_NINE, | |
THIRTY, | |
THIRTY_ONE, | |
THIRTY_TWO, | |
THIRTY_THREE, | |
THIRTY_FOUR, | |
THIRTY_FIVE, | |
THIRTY_SIX, | |
THIRTY_SEVEN, | |
THIRTY_EIGHT, | |
THIRTY_NINE, | |
FOURTY, | |
FOURTY_ONE, | |
FOURTY_TWO, | |
FOURTY_THREE, | |
FOURTY_FOUR, | |
FOURTY_FIVE, | |
FOURTY_SIX, | |
FOURTY_SEVEN, | |
FOURTY_EIGHT, | |
FOURTY_NINE, | |
FIFTY, | |
FIFTY_ONE, | |
FIFTY_TWO, | |
FIFTY_THREE, | |
FIFTY_FOUR, | |
FIFTY_FIVE, | |
FIFTY_SIX, | |
FIFTY_SEVEN, | |
FIFTY_EIGHT, | |
FIFTY_NINE; | |
private static final Set<Test> VALUES = Collections.unmodifiableSet(EnumSet.allOf(Test.class)); | |
private static final Set<Test> IMM_VALUES = Sets.immutableEnumSet(VALUES); | |
public static Set<Test> valueSet() { | |
return VALUES; | |
} | |
public static Set<Test> immValueSet() { | |
return IMM_VALUES; | |
} | |
} | |
} |
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
Benchmark Mode Cnt Score Error Units | |
App.loopCachedEnumSet thrpt 100 2338974.424 ± 49457.351 ops/s | |
App.loopImmutableCachedEnumSet thrpt 100 2046184.595 ± 44597.542 ops/s | |
App.loopOverEnumSet thrpt 100 2425853.670 ± 66098.382 ops/s | |
App.loopOverValues thrpt 100 4127155.550 ± 70639.525 ops/s | |
App.streamCachedEnumSet thrpt 100 2719483.474 ± 23660.891 ops/s | |
App.streamEnumSet thrpt 100 2465233.458 ± 17277.023 ops/s | |
App.streamImmutableCachedEnumSet thrpt 100 2016854.902 ± 13839.292 ops/s | |
App.streamValues thrpt 100 3134433.767 ± 65449.563 ops/s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment