package username.tests; import java.io.PrintWriter; import username.analysis.Graph; import username.util.Mathematician; /** * A class that helps analyze the various exponentiation algorithms. * * @author Samuel A. Rebelsky * @version 1.1 of April 2006 */ public class AnalyzeExpt { // +-----------+----------------------------------------------- // | Constants | // +-----------+ /** * The number of times to repeat a test so that too-quick tests * aren't too fast to measure. */ private static final int REPETITIONS = 1000; /** * The largest n to use. */ private static final int MAXN = 1000; /** * The increment to n at each step. */ public static final int INCREMENT(int n) { return 2*n + 1; } // INCREMENT(int) // +-------+--------------------------------------------------- // | Tests | // +-------+ public static void main(String[] args) throws Exception { double x = 2.7; PrintWriter pen = new PrintWriter(System.out, true); Graph g = new Graph(70,30); // Try a variety of exponents for one version. pen.println("Analyzing exptFor"); pen.println("n\ttime in microseconds"); for (int n = 0; n < MAXN; n = INCREMENT(n)) { long start = System.currentTimeMillis(); for (int rep = 0; rep < REPETITIONS; rep++) { double result = Mathematician.exptFor(x,n); } // for long finish = System.currentTimeMillis(); long time = 1000*(finish-start)/REPETITIONS; pen.println(n + "\t" + time); g.plot(n,(int) time); } // for each exponent pen.println(); g.setPlotChar('+'); pen.println("Analyzing exptWhile"); pen.println("n\ttime in microseconds"); // Try a variety of exponents for one version. for (int n = 0; n < MAXN; n = INCREMENT(n)) { long start = System.currentTimeMillis(); for (int rep = 0; rep < REPETITIONS; rep++) { double result = Mathematician.exptWhile(x,n); } // for long finish = System.currentTimeMillis(); long time = 1000*(finish-start)/REPETITIONS; pen.println(n + "\t" + time); g.plot(n,(int) time); } // for each exponent pen.println(); // Draw the graph pen.println(g); } // main(String[]) } // class AnalyzeExpt