Skip to content

Instantly share code, notes, and snippets.

@orionll
Created August 6, 2019 05:56
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 orionll/40be6a5a08cb3df5b46b7faf035079f1 to your computer and use it in GitHub Desktop.
Save orionll/40be6a5a08cb3df5b46b7faf035079f1 to your computer and use it in GitHub Desktop.
import com.google.common.math.Stats;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Thread)
public class Statistics {
@Param({"10", "1000", "100000"})
int size;
int[] array;
@Setup
public void setUp() {
this.array = ThreadLocalRandom.current().ints().limit(this.size).toArray();
}
@Benchmark
public double sum() {
double sum = 0;
for (final int a : this.array) {
sum += a;
}
return sum;
}
@Benchmark
public double sumStats() {
return Stats.of(this.array).sum();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment