Skip to content

Instantly share code, notes, and snippets.

@si14
Last active August 29, 2015 14:05
Show Gist options
  • Save si14/6b909a3c23b57a9cc0e3 to your computer and use it in GitHub Desktop.
Save si14/6b909a3c23b57a9cc0e3 to your computer and use it in GitHub Desktop.
import org.apache.commons.math3.util.FastMath;
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.Random;
import java.util.concurrent.TimeUnit;
import java.util.function.DoubleUnaryOperator;
/**
* @author Dmitry Groshev
* @date 11/08/2014
*/
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@State(Scope.Benchmark)
public class ExpBenchmark {
int arrayLen = 1000;
double[] logValues;
double[] values;
@Setup
public void prepare() {
logValues = new double[arrayLen];
final Random gen = new Random(42);
for (int i = 0; i < arrayLen; i++) {
logValues[i] = gen.nextDouble();
}
transformInPlace(logValues, Math::log);
}
public static double[] transform(final double[] values, final DoubleUnaryOperator op) {
final double[] res = values.clone();
transformInPlace(res, op);
return res;
}
public static void transformInPlace(final double[] values, final DoubleUnaryOperator op) {
for (int i = 0; i < values.length; i++) {
values[i] = op.applyAsDouble(values[i]);
}
}
@Benchmark
public void jvmMathExpArray() {
values = transform(logValues, Math::exp);
}
@Benchmark
public void fastMathExpArray() {
values = transform(logValues, FastMath::exp);
}
public static void main(final String[] args) throws RunnerException {
final Options opts = new OptionsBuilder()
.include(".*" + ExpBenchmark.class.getSimpleName() + ".*")
.forks(1)
.build();
new Runner(opts).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment