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