Skip to content

Instantly share code, notes, and snippets.

@entzik
Created December 17, 2016 22:59
Show Gist options
  • Save entzik/4ccb5cd7db1a61d0290f4285a22bdf96 to your computer and use it in GitHub Desktop.
Save entzik/4ccb5cd7db1a61d0290f4285a22bdf96 to your computer and use it in GitHub Desktop.
public class ObjectStreamAverageBenchmark {
@State(Scope.Thread)
public static class BenchmarkState {
public ArrayList<Record> records;
public BenchmarkState() {
records = new ArrayList<>(1000000);
for (int i = 0; i < 1000000; i ++)
records.add(new Record(i, "name" + i, Math.random()));
}
}
@Benchmark
@BenchmarkMode(Mode.All)
public void benchmark(BenchmarkState state, Blackhole bh) {
bh.consume(state.records.stream().map(Record::getValue).collect(Collectors.averagingDouble(d -> d)).doubleValue());
}
static class Record {
long id;
String name;
double value;
public Record(long id, String name, double value) {
this.id = id;
}
public long getId() {
return id;
}
public Record setId(long id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public Record setName(String name) {
this.name = name;
return this;
}
public double getValue() {
return value;
}
public Record setValue(double value) {
this.value = value;
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment