/* T02n05 - The same content as T02n03.java, but with 'wall-clock' * timing code added. * * Java was designed to be system independent. For that reason, we * can't use standard Java and still access system-dependent features, * such as CPU timing information. CPU timing information would tell * us how much time the CPU spent working on our program. * * Instead, we must live with 'wall-clock' time, which is the amount of * time that elapsed on the clock while the code was executing. This * can vary quite a bit based on the system's load. Thus, to get * reasonable comparisons, the code being compared should be executed * repeatedly, and at a time when the system is as lightly-loaded as possible. * * This program uses Java's System.currentTimeMillis() method to record * the current time before and after the calls to each of the versions of * the algorithm. Java 1.5 added System.nanoTime() for programmers needing * finer timing granularity. * * Compare the timing results from this program's execution with the * profiling results from T02n03. Are they similar? */ import java.io.*; public class T02n05 { private static void createList(double [] list, int size) { for (int i=0; i"); System.exit(1); } else { listSize = Integer.parseInt(args[0]); } list = new double [listSize]; createList(list,listSize); System.out.println("n = " + list.length); startTime = startTiming(); minMaxV1(list, list.length, minMax); seconds = stopTiming(startTime); System.out.println("minMaxV1: min = " + minMax.getMinValue() + ", max = " + minMax.getMaxValue() + " in " + seconds + " seconds."); startTime = startTiming(); minMaxV2(list, list.length, minMax); seconds = stopTiming(startTime); System.out.println("minMaxV2: min = " + minMax.getMinValue() + ", max = " + minMax.getMaxValue() + " in " + seconds + " seconds."); startTime = startTiming(); minMaxV3(list, list.length, minMax); seconds = stopTiming(startTime); System.out.println("minMaxV3: min = " + minMax.getMinValue() + ", max = " + minMax.getMaxValue() + " in " + seconds + " seconds."); } } class MinMax { private double minimum, maximum; public MinMax () { minimum = Double.POSITIVE_INFINITY; maximum = Double.NEGATIVE_INFINITY; } public MinMax (double minValue, double maxValue) { minimum = minValue; maximum = maxValue; } public double getMinValue () { return minimum; } public double getMaxValue () { return maximum; } public void setMinValue (double newMinValue) { minimum = newMinValue; } public void setMaxValue (double newMaxValue) { maximum = newMaxValue; } }