Last active
September 3, 2017 18:48
-
-
Save babanin/112d8ea84c34ac2e2c1b4556659d0699 to your computer and use it in GitHub Desktop.
Benchmarks different types of for-cycle in Groovy 2.4.7
This file contains hidden or 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 ib.groovy | |
import org.openjdk.jmh.annotations.Benchmark | |
import org.openjdk.jmh.annotations.Fork | |
import org.openjdk.jmh.annotations.Measurement | |
import org.openjdk.jmh.annotations.OutputTimeUnit | |
import org.openjdk.jmh.annotations.Scope | |
import org.openjdk.jmh.annotations.Setup | |
import org.openjdk.jmh.annotations.State | |
import org.openjdk.jmh.annotations.Warmup | |
import org.openjdk.jmh.infra.Blackhole | |
import java.util.concurrent.TimeUnit | |
@Fork(value = 4, warmups = 2) | |
@Measurement(iterations = 8) | |
@Warmup(iterations = 4) | |
@OutputTimeUnit(TimeUnit.MILLISECONDS) | |
@State(Scope.Benchmark) | |
class ForBenchmarks { | |
private List<Integer> values; | |
@Setup | |
void setup() { | |
values = new ArrayList<>(1 << 17) | |
for (int i = 0; i < values.size(); i++) { | |
values.add(i); | |
} | |
} | |
@Benchmark | |
void testEach(Blackhole bh) { | |
values.each { bh.consume(it) } | |
} | |
@Benchmark | |
void testForIn(Blackhole bh) { | |
for (i in values) { | |
bh.consume(i) | |
} | |
} | |
@Benchmark | |
void testForEach(Blackhole bh) { | |
for (Integer i : values) { | |
bh.consume(i) | |
} | |
} | |
@Benchmark | |
void testFor(Blackhole bh) { | |
for (int i = 0; i < values.size(); i++) { | |
bh.consume(values.get(i)) | |
} | |
} | |
} | |
/** | |
Benchmark Mode Cnt Score Error Units | |
ForBenchmarks.testEach thrpt 32 10889.306 ± 406.861 ops/ms | |
ForBenchmarks.testFor thrpt 32 105201.557 ± 4435.814 ops/ms | |
ForBenchmarks.testForEach thrpt 32 138256.978 ± 1699.157 ops/ms | |
ForBenchmarks.testForIn thrpt 32 137114.933 ± 2802.429 ops/ms | |
i7-4770, 32Gb RAM | |
Benchmark Mode Cnt Score Error Units | |
ForBenchmarks.testEach thrpt 32 10983.740 ± 536.228 ops/ms | |
ForBenchmarks.testFor thrpt 32 121392.489 ± 1944.488 ops/ms | |
ForBenchmarks.testForEach thrpt 32 162295.392 ± 3246.866 ops/ms | |
ForBenchmarks.testForIn thrpt 32 156622.444 ± 5791.028 ops/ms | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment