Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Last active May 3, 2019 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-neck/944c5e3458de3c24fee7fb6bf0f3a206 to your computer and use it in GitHub Desktop.
Save mike-neck/944c5e3458de3c24fee7fb6bf0f3a206 to your computer and use it in GitHub Desktop.
package com.example;
import org.openjdk.jmh.annotations.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
import java.util.stream.Stream;
@State(Scope.Group)
public class AddOperationBenchmark {
private List<String > arrayList;
private List<String> linkedList;
@Group
@Setup(Level.Iteration)
public void setup() {
this.arrayList = new ArrayList<>(1_000_000);
this.linkedList = new LinkedList<>();
for (int i = 0; i < 1_000_000; i++) {
String s = randomString();
arrayList.add(s);
linkedList.add(s);
}
}
@Group
@Benchmark
@BenchmarkMode({Mode.AverageTime, Mode.Throughput})
public void arrayList() {
runAction(arrayList::add);
}
@Group
@Benchmark
@BenchmarkMode({Mode.AverageTime, Mode.Throughput})
public void linkedList() {
runAction(linkedList::add);
}
private static void runAction(Consumer<? super String> action) {
for (int i = 0; i < 10; i++) {
String s = randomString();
action.accept(s);
}
}
private static final Random random = new Random(1000L);
static int index(int max) {
return random.nextInt(max);
}
static String randomString() {
return Stream.generate(() -> Memory.Chars.fromRandom(random))
.limit(random.nextInt(512) + 1L)
.reduce(new StringBuilder(), (builder, ch) -> builder.append(ch.ch), StringBuilder::append)
.toString();
}
}
package com.example;
import org.openjdk.jmh.annotations.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.IntFunction;
@State(Scope.Group)
public class GetOperationBenchmark {
private List<String> arrayList;
private List<String> linkedList;
@Group
@Setup(Level.Invocation)
public void setup() {
this.arrayList = Memory.ListFactory.ARRAY_LIST.newList();
this.linkedList = Memory.ListFactory.LINKED_LIST.newList();
for (int i = 0; i < 1_000_000; i++) {
String s = AddOperationBenchmark.randomString();
arrayList.add(s);
linkedList.add(s);
}
}
@Group
@Benchmark
@BenchmarkMode({Mode.AverageTime, Mode.Throughput})
public void arrayList() {
runBenchmark(arrayList::get);
}
@Group
@Benchmark
@BenchmarkMode({Mode.AverageTime, Mode.Throughput})
public void linkedList() {
runBenchmark(linkedList::get);
}
private void runBenchmark(IntFunction<String> supplier) {
List<String> list = new ArrayList<>(100);
while (list.size() < 100) {
String s = supplier.apply(AddOperationBenchmark.index(1_000_000));
list.add(s);
}
}
}
Benchmark Mode Cnt Score Error Units
AddOperation.group thrpt 20 6.113 ± 0.403 ops/ms
AddOperation.group:arrayList thrpt 20 3.056 ± 0.201 ops/ms
AddOperation.group:linkedList thrpt 20 3.057 ± 0.203 ops/ms
GetOperation.group thrpt 20 56.179 ± 1.823 ops/ms
GetOperation.group:arrayList thrpt 20 56.168 ± 1.823 ops/ms
GetOperation.group:linkedList thrpt 20 0.011 ± 0.001 ops/ms
InsertOperation.group thrpt 20 1.165 ± 0.030 ops/ms
InsertOperation.group:arrayList thrpt 20 1.072 ± 0.021 ops/ms
InsertOperation.group:linkedList thrpt 20 0.093 ± 0.014 ops/ms
IterationOperation.group thrpt 20 0.167 ± 0.034 ops/ms
IterationOperation.group:arrayList thrpt 20 0.118 ± 0.003 ops/ms
IterationOperation.group:linkedList thrpt 20 0.050 ± 0.032 ops/ms
AddOperation.group avgt 20 0.277 ± 0.004 ms/op
AddOperation.group:arrayList avgt 20 0.276 ± 0.005 ms/op
AddOperation.group:linkedList avgt 20 0.277 ± 0.004 ms/op
GetOperation.group avgt 20 45.608 ± 1.495 ms/op
GetOperation.group:arrayList avgt 20 0.020 ± 0.009 ms/op
GetOperation.group:linkedList avgt 20 91.197 ± 2.992 ms/op
InsertOperation.group avgt 20 6.345 ± 1.048 ms/op
InsertOperation.group:arrayList avgt 20 0.926 ± 0.024 ms/op
InsertOperation.group:linkedList avgt 20 11.764 ± 2.075 ms/op
IterationOperation.group avgt 20 18.204 ± 4.923 ms/op
IterationOperation.group:arrayList avgt 20 8.502 ± 0.260 ms/op
IterationOperation.group:linkedList avgt 20 27.906 ± 9.607 ms/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment