Skip to content

Instantly share code, notes, and snippets.

@zivce
Last active May 25, 2021 20:18
Show Gist options
  • Save zivce/8f2a3d3be193eac2a00118640407b016 to your computer and use it in GitHub Desktop.
Save zivce/8f2a3d3be193eac2a00118640407b016 to your computer and use it in GitHub Desktop.
// excerpt from the benchmark
@Warmup(iterations = 50, time = 2, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 3, time = 2, timeUnit = TimeUnit.MILLISECONDS)
@Fork(1)
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@BenchmarkMode({ Mode.AverageTime})
public class Collections9 {
private static ArrayList<Cat> cats = new ArrayList<>();
private static List<String> cats9;
private static List<String> cats8;
@Benchmark
public void catsNonJava9Way(){
addCat("George");
addCat("John");
addCat("Mack");
addCat("Other");
addCat("Lorem");
for(Cat cat : cats) {
cat.print();
}
}
@Benchmark
public void catsJava8Way() {
// as seen on https://openjdk.java.net/jeps/269
cats8 = Collections.unmodifiableList(Stream.of("George", "John", "Mack", "Other", "Lorem").collect(Collectors.toList()));
cats8.forEach(System.out::println);
}
@Benchmark
public void catsJava9Way(){
cats9 = List.of("George", "John", "Mack", "Other", "Lorem");
cats9.forEach(System.out::println);
}
public void addCat(String name) {
cats.add(new Cat(name));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment