Skip to content

Commit

Permalink
Add JMH test for 'MathExpr'.
Browse files Browse the repository at this point in the history
  • Loading branch information
jenetics committed Apr 18, 2020
1 parent 3418efd commit 8f0154d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
5 changes: 3 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,5 +1,6 @@
#Sat Apr 18 22:21:01 CEST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
14 changes: 14 additions & 0 deletions jenetics.prog/build.gradle
Expand Up @@ -27,6 +27,10 @@
* @version 4.4
*/

plugins {
id 'me.champeau.gradle.jmh' version '0.5.0'
}

apply plugin: 'java-library'
apply plugin: 'packaging'
apply plugin: 'nexus'
Expand All @@ -41,6 +45,16 @@ dependencies {
testImplementation property('include.EqualsVerifier')
}

jmh {
include = ['.*MathExprPerf.*']
}

idea {
module{
scopes.COMPILE.plus += [configurations.jmh]
}
}

javadoc {
options {
links 'https://jenetics.io/javadoc/jenetics',
Expand Down
90 changes: 90 additions & 0 deletions jenetics.prog/src/jmh/java/io/jenetics/prog/op/MathExprPerf.java
@@ -0,0 +1,90 @@
/*
* Java Genetic Algorithm Library (@__identifier__@).
* Copyright (c) @__year__@ Franz Wilhelmstötter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author:
* Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
*/
package io.jenetics.prog.op;

import static java.lang.Math.asin;
import static java.lang.Math.ceil;
import static java.lang.Math.cos;
import static java.lang.Math.cosh;
import static java.lang.Math.floor;
import static java.lang.Math.hypot;
import static java.lang.Math.log;
import static java.lang.Math.rint;
import static java.lang.Math.signum;
import static java.lang.Math.sinh;
import static java.lang.Math.sqrt;
import static java.lang.Math.tan;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

/**
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
*/
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(value = 3)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class MathExprPerf {

private static final MathExpr MATH_EXPR = MathExpr.parse(
"cos(signum(tan(sqrt(asin(rint(sinh(log(floor(log(hypot(cosh(sinh(log(y)%hypot(y, 1.0))), " +
"signum(tan(ceil(ceil(y)))))))))))))))"
);

private static double expr(final double x, final double y) {
return cos(signum(tan(sqrt(asin(rint(sinh(log(floor(log(hypot(cosh(sinh(log(y)%
hypot(y, 1.0))), signum(tan(ceil(ceil(y)))))))))))))));
}

double x;
double y;

@Setup
public void setup() {
final Random random = new Random();
x = random.nextDouble()*10;
y = random.nextDouble();
}

@Benchmark
public double mathExpr() {
return MATH_EXPR.eval(x, y);
}

@Benchmark
public double javaExpr() {
return expr(x, y);
}

}

0 comments on commit 8f0154d

Please sign in to comment.