aoqi@0: /* aoqi@0: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * Micro-benchmark for Math.pow() and Math.exp() aoqi@0: */ aoqi@0: aoqi@0: import java.util.*; aoqi@0: aoqi@0: public class Test7177917 { aoqi@0: aoqi@0: static double d; aoqi@0: aoqi@0: static Random r = new Random(0); aoqi@0: aoqi@0: static long m_pow(double[][] values) { aoqi@0: double res = 0; aoqi@0: long start = System.nanoTime(); aoqi@0: for (int i = 0; i < values.length; i++) { aoqi@0: res += Math.pow(values[i][0], values[i][1]); aoqi@0: } aoqi@0: long stop = System.nanoTime(); aoqi@0: d = res; aoqi@0: return (stop - start) / 1000; aoqi@0: } aoqi@0: aoqi@0: static long m_exp(double[] values) { aoqi@0: double res = 0; aoqi@0: long start = System.nanoTime(); aoqi@0: for (int i = 0; i < values.length; i++) { aoqi@0: res += Math.exp(values[i]); aoqi@0: } aoqi@0: long stop = System.nanoTime(); aoqi@0: d = res; aoqi@0: return (stop - start) / 1000; aoqi@0: } aoqi@0: aoqi@0: static double[][] pow_values(int nb) { aoqi@0: double[][] res = new double[nb][2]; aoqi@0: for (int i = 0; i < nb; i++) { aoqi@0: double ylogx = (1 + (r.nextDouble() * 2045)) - 1023; // 2045 rather than 2046 as a safety margin aoqi@0: double x = Math.abs(Double.longBitsToDouble(r.nextLong())); aoqi@0: while (x != x) { aoqi@0: x = Math.abs(Double.longBitsToDouble(r.nextLong())); aoqi@0: } aoqi@0: double logx = Math.log(x) / Math.log(2); aoqi@0: double y = ylogx / logx; aoqi@0: aoqi@0: res[i][0] = x; aoqi@0: res[i][1] = y; aoqi@0: } aoqi@0: return res; aoqi@0: } aoqi@0: aoqi@0: static double[] exp_values(int nb) { aoqi@0: double[] res = new double[nb]; aoqi@0: for (int i = 0; i < nb; i++) { aoqi@0: double ylogx = (1 + (r.nextDouble() * 2045)) - 1023; // 2045 rather than 2046 as a safety margin aoqi@0: double x = Math.E; aoqi@0: double logx = Math.log(x) / Math.log(2); aoqi@0: double y = ylogx / logx; aoqi@0: res[i] = y; aoqi@0: } aoqi@0: return res; aoqi@0: } aoqi@0: aoqi@0: static public void main(String[] args) { aoqi@0: { aoqi@0: // warmup aoqi@0: double[][] warmup_values = pow_values(10); aoqi@0: m_pow(warmup_values); aoqi@0: aoqi@0: for (int i = 0; i < 20000; i++) { aoqi@0: m_pow(warmup_values); aoqi@0: } aoqi@0: // test pow perf aoqi@0: double[][] values = pow_values(1000000); aoqi@0: System.out.println("==> POW " + m_pow(values)); aoqi@0: aoqi@0: // force uncommon trap aoqi@0: double[][] nan_values = new double[1][2]; aoqi@0: nan_values[0][0] = Double.NaN; aoqi@0: nan_values[0][1] = Double.NaN; aoqi@0: m_pow(nan_values); aoqi@0: aoqi@0: // force recompilation aoqi@0: for (int i = 0; i < 20000; i++) { aoqi@0: m_pow(warmup_values); aoqi@0: } aoqi@0: aoqi@0: // test pow perf again aoqi@0: System.out.println("==> POW " + m_pow(values)); aoqi@0: } aoqi@0: { aoqi@0: // warmup aoqi@0: double[] warmup_values = exp_values(10); aoqi@0: m_exp(warmup_values); aoqi@0: aoqi@0: for (int i = 0; i < 20000; i++) { aoqi@0: m_exp(warmup_values); aoqi@0: } aoqi@0: aoqi@0: // test pow perf aoqi@0: double[] values = exp_values(1000000); aoqi@0: System.out.println("==> EXP " + m_exp(values)); aoqi@0: aoqi@0: // force uncommon trap aoqi@0: double[] nan_values = new double[1]; aoqi@0: nan_values[0] = Double.NaN; aoqi@0: m_exp(nan_values); aoqi@0: aoqi@0: // force recompilation aoqi@0: for (int i = 0; i < 20000; i++) { aoqi@0: m_exp(warmup_values); aoqi@0: } aoqi@0: aoqi@0: // test pow perf again aoqi@0: System.out.println("==> EXP " + m_exp(values)); aoqi@0: } aoqi@0: } aoqi@0: }