/* * FibTest.java * * Contains several different methods to compute the Fibonacci numbers with the * only difference being how many local variables they allocated and in what order * Designed to demonstrate stack alignment performance problems for double calculations * * This is a microbenchmark and as such does not compute anything useful or in a very * intelligent way. It does however clearly demonstrate the performance impact of * misaligned double variables. * * Created on March 5, 2002, 5:34 PM */ public final class FibTest { /** Creates a new instance of FibTest */ public FibTest() { } double computeFib(double inNum) { if (inNum <= 2) return 1; double prev2 = computeFib(inNum-2); double prev1 = computeFib(inNum-1); return prev1 + prev2; } static double computeFibStatic(double inNum) { if (inNum <= 2) return 1; double prev2 = computeFibStatic(inNum-2); double prev1 = computeFibStatic(inNum-1); return prev1 + prev2; } double computeFibWithFirstInt(double inNum) { if (inNum <= 2) return 1; int dummy; double prev2 = computeFibWithFirstInt(inNum-2); double prev1 = computeFibWithFirstInt(inNum-1); return prev1 + prev2; } double computeFibWithMiddleInt(double inNum) { if (inNum <= 2) return 1; double prev2 = computeFibWithMiddleInt(inNum-2); int dummy; double prev1 = computeFibWithMiddleInt(inNum-1); return prev1 + prev2; } /** Simple method that prints the time to sum the first 32 Fibonacci numbers. * The fibonacci numbers are all computed using a simple recursive formulation * with the only difference being the number of local variables allocated on the * the stack and the ordering of those variables. * Includes warmup iteration of each timing routine to ensure that dynamic * compilation can occur before timings are reported. * * @param args the command line arguments */ public static void main(String[] args) { FibTest fib = new FibTest(); final int maxFibNum = 32; final int numIterations = 10; final int numRepeatedTests = 5; final int numNoReport = 2; double sum=0; //comment or uncomment this dummy int variable to see time changes //int dummyInt; //test using doubles for(int k=0;k=numNoReport) System.out.println("computeFib test took "+(time/1000.0f)+" secs sum = "+sum); } //test using doubles with an extra int local variable per function for(int k=0;k=numNoReport) System.out.println("computeFibWithFirstInt test took "+(time/1000.0f)+" secs sum = "+sum); } //test using doubles with an extra int local variable per function for(int k=0;k=numNoReport) System.out.println("computeFibWithMiddleInt test took "+(time/1000.0f)+" secs sum = "+sum); } //test using doubles in static method for(int k=0;k=numNoReport) System.out.println("computeFibStatic test took "+(time/1000.0f)+" secs sum = "+sum); } } }