From febcb077e7da0b64607b3063df53824987b68bef Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Sun, 23 Jan 2011 15:44:32 +0000 Subject: [PATCH] Split testPerformance() into new class git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1062456 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/math/util/FastMathTest.java | 211 ---------------- .../math/util/FastMathTestPerformance.java | 238 ++++++++++++++++++ 2 files changed, 238 insertions(+), 211 deletions(-) create mode 100644 src/test/java/org/apache/commons/math/util/FastMathTestPerformance.java diff --git a/src/test/java/org/apache/commons/math/util/FastMathTest.java b/src/test/java/org/apache/commons/math/util/FastMathTest.java index 61d31a595..6305c8808 100644 --- a/src/test/java/org/apache/commons/math/util/FastMathTest.java +++ b/src/test/java/org/apache/commons/math/util/FastMathTest.java @@ -1212,215 +1212,4 @@ public class FastMathTest { compareClassMethods( FastMath.class, StrictMath.class); } - @Ignore - @Test - public void testPerformance() { - final int numberOfRuns = 10000000; - for (int j = 0; j < 10; j++) { - double x = 0; - long time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += StrictMath.log(Math.PI + i/* 1.0 + i/1e9 */); - time = System.currentTimeMillis() - time; - System.out.print("StrictMath.log " + time + "\t" + x + "\t"); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.log(Math.PI + i/* 1.0 + i/1e9 */); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.log " + time + "\t" + x); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += StrictMath.pow(Math.PI + i / 1e6, i / 1e6); - time = System.currentTimeMillis() - time; - System.out.print("StrictMath.pow " + time + "\t" + x + "\t"); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.pow(Math.PI + i / 1e6, i / 1e6); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.pow " + time + "\t" + x); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += StrictMath.exp(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.print("StrictMath.exp " + time + "\t" + x + "\t"); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.exp(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.exp " + time + "\t" + x); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += StrictMath.sin(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.print("StrictMath.sin " + time + "\t" + x + "\t"); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.sin(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.sin " + time + "\t" + x); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += StrictMath.asin(i / 10000000.0); - time = System.currentTimeMillis() - time; - System.out.print("StrictMath.asin " + time + "\t" + x + "\t"); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.asin(i / 10000000.0); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.asin " + time + "\t" + x); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += StrictMath.cos(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.print("StrictMath.cos " + time + "\t" + x + "\t"); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.cos(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.cos " + time + "\t" + x); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += StrictMath.acos(i / 10000000.0); - time = System.currentTimeMillis() - time; - System.out.print("StrictMath.acos " + time + "\t" + x + "\t"); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.acos(i / 10000000.0); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.acos " + time + "\t" + x); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += StrictMath.tan(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.print("StrictMath.tan " + time + "\t" + x + "\t"); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.tan(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.tan " + time + "\t" + x); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += StrictMath.atan(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.print("StrictMath.atan " + time + "\t" + x + "\t"); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.atan(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.atan " + time + "\t" + x); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += StrictMath.cbrt(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.print("StrictMath.cbrt " + time + "\t" + x + "\t"); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.cbrt(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.cbrt " + time + "\t" + x); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += StrictMath.cosh(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.print("StrictMath.cosh " + time + "\t" + x + "\t"); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.cosh(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.cosh " + time + "\t" + x); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += StrictMath.sinh(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.print("StrictMath.sinh " + time + "\t" + x + "\t"); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.sinh(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.sinh " + time + "\t" + x); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += StrictMath.tanh(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.print("StrictMath.tanh " + time + "\t" + x + "\t"); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.tanh(i / 1000000.0); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.tanh " + time + "\t" + x); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += StrictMath.expm1(-i / 100000.0); - time = System.currentTimeMillis() - time; - System.out.print("StrictMath.expm1 " + time + "\t" + x + "\t"); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.expm1(-i / 100000.0); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.expm1 " + time + "\t" + x); - - x = 0; - time = System.currentTimeMillis(); - for (int i = 0; i < numberOfRuns; i++) - x += FastMath.expm1(-i / 100000.0); - time = System.currentTimeMillis() - time; - System.out.println("FastMath.expm1 " + time + "\t" + x); - - } - } - } diff --git a/src/test/java/org/apache/commons/math/util/FastMathTestPerformance.java b/src/test/java/org/apache/commons/math/util/FastMathTestPerformance.java new file mode 100644 index 000000000..cc35e076a --- /dev/null +++ b/src/test/java/org/apache/commons/math/util/FastMathTestPerformance.java @@ -0,0 +1,238 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.commons.math.util; + +import org.junit.Test; + +/** + * Performance tests for FastMath. + * Not enabled by default, as the class does not have Test in its name. + * + * Invoke by running {@code mvn test -Dtest=FastMathTestPerformance} + */ +public class FastMathTestPerformance { + @Test + public void testPerformance() { + final int numberOfRuns = 10000000; + for (int j = 0; j < 10; j++) { + double x = 0; + long time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.log(Math.PI + i/* 1.0 + i/1e9 */); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.log " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.log(Math.PI + i/* 1.0 + i/1e9 */); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.log " + time + "\t" + x); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.pow(Math.PI + i / 1e6, i / 1e6); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.pow " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.pow(Math.PI + i / 1e6, i / 1e6); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.pow " + time + "\t" + x); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.exp(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.exp " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.exp(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.exp " + time + "\t" + x); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.sin(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.sin " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.sin(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.sin " + time + "\t" + x); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.asin(i / 10000000.0); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.asin " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.asin(i / 10000000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.asin " + time + "\t" + x); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.cos(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.cos " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.cos(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.cos " + time + "\t" + x); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.acos(i / 10000000.0); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.acos " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.acos(i / 10000000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.acos " + time + "\t" + x); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.tan(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.tan " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.tan(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.tan " + time + "\t" + x); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.atan(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.atan " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.atan(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.atan " + time + "\t" + x); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.cbrt(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.cbrt " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.cbrt(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.cbrt " + time + "\t" + x); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.cosh(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.cosh " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.cosh(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.cosh " + time + "\t" + x); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.sinh(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.sinh " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.sinh(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.sinh " + time + "\t" + x); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.tanh(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.tanh " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.tanh(i / 1000000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.tanh " + time + "\t" + x); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.expm1(-i / 100000.0); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.expm1 " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.expm1(-i / 100000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.expm1 " + time + "\t" + x); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.expm1(-i / 100000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.expm1 " + time + "\t" + x); + + } + } + +} \ No newline at end of file