Skip to content

Instantly share code, notes, and snippets.

@isopov
Created May 30, 2023 08:45
Show Gist options
  • Save isopov/285860c112b28689940b1e347de8ab7a to your computer and use it in GitHub Desktop.
Save isopov/285860c112b28689940b1e347de8ab7a to your computer and use it in GitHub Desktop.
eclipse collections megamorphic calls benchmark
package io.github.isopov.jmh;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.impl.factory.Lists;
import org.openjdk.jmh.annotations.*;
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.List;
import java.util.concurrent.TimeUnit;
//Result is
//Benchmark Mode Cnt Score Error Units
//ListOfBenchmark.eclipseSize avgt 15 64.236 ± 0.827 ns/op
//ListOfBenchmark.jdkSize avgt 15 8.536 ± 0.068 ns/op
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@Fork(3)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 3, time = 1)
@Measurement(iterations = 5, time = 1)
public class ListOfBenchmark {
private ImmutableList<ImmutableList<String>> eclipse = Lists.immutable.of(
Lists.immutable.of("a"),
Lists.immutable.of("a", "b"),
Lists.immutable.of("a", "b", "c"),
Lists.immutable.of("a", "b", "c", "d"),
Lists.immutable.of("a", "b", "c", "d", "e"),
Lists.immutable.of("a", "b", "c", "d", "e", "f"),
Lists.immutable.of("a", "b", "c", "d", "e", "f", "g")
);
private List<List<String>> jdk = List.of(
List.of("a"),
List.of("a", "b"),
List.of("a", "b", "c"),
List.of("a", "b", "c", "d"),
List.of("a", "b", "c", "d", "e"),
List.of("a", "b", "c", "d", "e", "f"),
List.of("a", "b", "c", "d", "e", "f", "g")
);
@Benchmark
public int eclipseSize() {
int result = 0;
for (var l : eclipse) {
result += l.size();
}
return result;
}
@Benchmark
public int jdkSize() {
int result = 0;
for (var l : jdk) {
result += l.size();
}
return result;
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(".*" + ListOfBenchmark.class.getSimpleName() + ".*")
.build();
new Runner(opt).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment