1 /* 2 * Copyright (c) 2012, 2015, 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 * Micro-benchmark for Math.pow() and Math.exp() 27 */ 28 29 import jdk.test.lib.Utils; 30 import java.util.Random; 31 32 public class Test7177917 { 33 34 static double d; 35 36 static final Random R = Utils.getRandomInstance(); 37 38 static long m_pow(double[][] values) { 39 double res = 0; 40 long start = System.nanoTime(); 41 for (int i = 0; i < values.length; i++) { 42 res += Math.pow(values[i][0], values[i][1]); 43 } 44 long stop = System.nanoTime(); 45 d = res; 46 return (stop - start) / 1000; 47 } 48 49 static long m_exp(double[] values) { 50 double res = 0; 51 long start = System.nanoTime(); 52 for (int i = 0; i < values.length; i++) { 53 res += Math.exp(values[i]); 54 } 55 long stop = System.nanoTime(); 56 d = res; 57 return (stop - start) / 1000; 58 } 59 60 static double[][] pow_values(int nb) { 61 double[][] res = new double[nb][2]; 62 for (int i = 0; i < nb; i++) { 63 double ylogx = (1 + (R.nextDouble() * 2045)) - 1023; // 2045 rather than 2046 as a safety margin 64 double x = Math.abs(Double.longBitsToDouble(R.nextLong())); 65 while (x != x) { 66 x = Math.abs(Double.longBitsToDouble(R.nextLong())); 67 } 68 double logx = Math.log(x) / Math.log(2); 69 double y = ylogx / logx; 70 71 res[i][0] = x; 72 res[i][1] = y; 73 } 74 return res; 75 } 76 77 static double[] exp_values(int nb) { 78 double[] res = new double[nb]; 79 for (int i = 0; i < nb; i++) { 80 double ylogx = (1 + (R.nextDouble() * 2045)) - 1023; // 2045 rather than 2046 as a safety margin 81 double x = Math.E; 82 double logx = Math.log(x) / Math.log(2); 83 double y = ylogx / logx; 84 res[i] = y; 85 } 86 return res; 87 } 88 89 static public void main(String[] args) { 90 { 91 // warmup 92 double[][] warmup_values = pow_values(10); 93 m_pow(warmup_values); 94 95 for (int i = 0; i < 20000; i++) { 96 m_pow(warmup_values); 97 } 98 // test pow perf 99 double[][] values = pow_values(1000000); 100 System.out.println("==> POW " + m_pow(values)); 101 102 // force uncommon trap 103 double[][] nan_values = new double[1][2]; 104 nan_values[0][0] = Double.NaN; 105 nan_values[0][1] = Double.NaN; 106 m_pow(nan_values); 107 108 // force recompilation 109 for (int i = 0; i < 20000; i++) { 110 m_pow(warmup_values); 111 } 112 113 // test pow perf again 114 System.out.println("==> POW " + m_pow(values)); 115 } 116 { 117 // warmup 118 double[] warmup_values = exp_values(10); 119 m_exp(warmup_values); 120 121 for (int i = 0; i < 20000; i++) { 122 m_exp(warmup_values); 123 } 124 125 // test pow perf 126 double[] values = exp_values(1000000); 127 System.out.println("==> EXP " + m_exp(values)); 128 129 // force uncommon trap 130 double[] nan_values = new double[1]; 131 nan_values[0] = Double.NaN; 132 m_exp(nan_values); 133 134 // force recompilation 135 for (int i = 0; i < 20000; i++) { 136 m_exp(warmup_values); 137 } 138 139 // test pow perf again 140 System.out.println("==> EXP " + m_exp(values)); 141 } 142 } 143 }