1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 /** 26 * @test 27 * @bug 7177917 28 * @summary Micro-benchmark for Math.pow() and Math.exp() 29 * 30 * @run main Test7177917 31 */ 32 33 import java.util.*; 34 35 public class Test7177917 { 36 37 static double d; 38 39 static Random r = new Random(0); 40 m_pow(double[][] values)41 static long m_pow(double[][] values) { 42 double res = 0; 43 long start = System.nanoTime(); 44 for (int i = 0; i < values.length; i++) { 45 res += Math.pow(values[i][0], values[i][1]); 46 } 47 long stop = System.nanoTime(); 48 d = res; 49 return (stop - start) / 1000; 50 } 51 m_exp(double[] values)52 static long m_exp(double[] values) { 53 double res = 0; 54 long start = System.nanoTime(); 55 for (int i = 0; i < values.length; i++) { 56 res += Math.exp(values[i]); 57 } 58 long stop = System.nanoTime(); 59 d = res; 60 return (stop - start) / 1000; 61 } 62 pow_values(int nb)63 static double[][] pow_values(int nb) { 64 double[][] res = new double[nb][2]; 65 for (int i = 0; i < nb; i++) { 66 double ylogx = (1 + (r.nextDouble() * 2045)) - 1023; // 2045 rather than 2046 as a safety margin 67 double x = Math.abs(Double.longBitsToDouble(r.nextLong())); 68 while (x != x) { 69 x = Math.abs(Double.longBitsToDouble(r.nextLong())); 70 } 71 double logx = Math.log(x) / Math.log(2); 72 double y = ylogx / logx; 73 74 res[i][0] = x; 75 res[i][1] = y; 76 } 77 return res; 78 } 79 exp_values(int nb)80 static double[] exp_values(int nb) { 81 double[] res = new double[nb]; 82 for (int i = 0; i < nb; i++) { 83 double ylogx = (1 + (r.nextDouble() * 2045)) - 1023; // 2045 rather than 2046 as a safety margin 84 double x = Math.E; 85 double logx = Math.log(x) / Math.log(2); 86 double y = ylogx / logx; 87 res[i] = y; 88 } 89 return res; 90 } 91 main(String[] args)92 static public void main(String[] args) { 93 { 94 // warmup 95 double[][] warmup_values = pow_values(10); 96 m_pow(warmup_values); 97 98 for (int i = 0; i < 20000; i++) { 99 m_pow(warmup_values); 100 } 101 // test pow perf 102 double[][] values = pow_values(1000000); 103 System.out.println("==> POW " + m_pow(values)); 104 105 // force uncommon trap 106 double[][] nan_values = new double[1][2]; 107 nan_values[0][0] = Double.NaN; 108 nan_values[0][1] = Double.NaN; 109 m_pow(nan_values); 110 111 // force recompilation 112 for (int i = 0; i < 20000; i++) { 113 m_pow(warmup_values); 114 } 115 116 // test pow perf again 117 System.out.println("==> POW " + m_pow(values)); 118 } 119 { 120 // warmup 121 double[] warmup_values = exp_values(10); 122 m_exp(warmup_values); 123 124 for (int i = 0; i < 20000; i++) { 125 m_exp(warmup_values); 126 } 127 128 // test pow perf 129 double[] values = exp_values(1000000); 130 System.out.println("==> EXP " + m_exp(values)); 131 132 // force uncommon trap 133 double[] nan_values = new double[1]; 134 nan_values[0] = Double.NaN; 135 m_exp(nan_values); 136 137 // force recompilation 138 for (int i = 0; i < 20000; i++) { 139 m_exp(warmup_values); 140 } 141 142 // test pow perf again 143 System.out.println("==> EXP " + m_exp(values)); 144 } 145 } 146 } 147