Use reflection to change the compute setting.

This allows the code to compile even though the field is private.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1169736 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2011-09-12 13:20:57 +00:00
parent 372764aef6
commit d312add3bf
1 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,65 @@
package org.apache.commons.math.util;
import java.lang.reflect.Field;
/*
* FastMath load test - requires that FastMath.USE_PRECOMPUTED_TABLES be set to non-final.
*
Sample output from:
java -cp target/classes;target/test-classes org.apache.commons.math.util.FastMathLoadCheck false false
java -cp target/classes;target/test-classes org.apache.commons.math.util.FastMathLoadCheck false true
java -cp target/classes;target/test-classes org.apache.commons.math.util.FastMathLoadCheck true false
java -cp target/classes;target/test-classes org.apache.commons.math.util.FastMathLoadCheck true true
Using exp(100); compute=false
new 12155456 9219 5308 5029 5587 5308 5029 5029 5029 5308
Using exp(100); compute=true
new 34929579 9499 5308 5308 5029 5029 5029 5308 5028 5029
Using max(0,0); compute=false
new 5029 3632 3073 3073 3073 3073 3073 3073 3073 3073
Using max(0,0); compute=true
new 5028 3911 3073 3073 3073 3073 3073 3073 3073 3073
*/
public class FastMathLoadCheck {
private static int LOOPS = 10;
private static boolean MAX = false;
private static boolean compute = false;
public static void main(String[] args) throws Exception {
if (args.length>0) MAX = Boolean.valueOf(args[0]);
if (args.length>1) compute = Boolean.valueOf(args[1]);
if (args.length>2) LOOPS = Integer.valueOf(args[2]);
p("Using "+ (MAX ? "max(0,0)" : "exp(100)") + "; compute=" + compute+"\n");
Field usePrecompute = FastMath.class.getDeclaredField("USE_PRECOMPUTED_TABLES");
usePrecompute.setAccessible(true);
if (usePrecompute.getBoolean(null) != compute) {
usePrecompute.setBoolean(null, compute);
}
usePrecompute.setAccessible(false);
test();
}
private static void test(){
p("new");
for(int i=0; i< LOOPS; i++){
p(" ");
long t1 = System.nanoTime();
if (MAX) {
FastMath.max(0,0);
} else {
FastMath.exp(100);
}
long t2 = System.nanoTime();
p("%10d",t2-t1);
}
p("\n");
}
private static void p(String format, Object p){
System.out.printf(format, p);
}
private static void p(Object p){
System.out.print(p);
}
}