diff --git a/mattrixwv/Stopwatch$1.class b/mattrixwv/Stopwatch$1.class new file mode 100644 index 0000000..03b25be Binary files /dev/null and b/mattrixwv/Stopwatch$1.class differ diff --git a/mattrixwv/Stopwatch$TIME_RESOLUTION.class b/mattrixwv/Stopwatch$TIME_RESOLUTION.class new file mode 100644 index 0000000..b1c465d Binary files /dev/null and b/mattrixwv/Stopwatch$TIME_RESOLUTION.class differ diff --git a/mattrixwv/Stopwatch.class b/mattrixwv/Stopwatch.class new file mode 100644 index 0000000..da3ba87 Binary files /dev/null and b/mattrixwv/Stopwatch.class differ diff --git a/mattrixwv/Stopwatch.java b/mattrixwv/Stopwatch.java new file mode 100644 index 0000000..c4c95b0 --- /dev/null +++ b/mattrixwv/Stopwatch.java @@ -0,0 +1,121 @@ +//Java/JavaClasses/Mattrixwv/Stopwatch.java +//Matthew Ellison (Mattrixwv) +// Created: 03-01-19 +//Modified: 03-01-19 +//This file contains a class that is used to time the execution time of other programs + + +package mattrixwv; + + +public class Stopwatch{ + private Long startTime; + private Long stopTime; + //Constructor makes sure all values are set to defaults + public Stopwatch(){ + //Make sure both values are null so it is easier to detect incorrect function calling order + startTime = null; + stopTime = null; + } + //Returns a long with the elapsed time in nanoseconds. Used by other functions to get the time before converting it to the correct resolution + private Long getTime(){ + if(startTime == null){ + ///This should throw an exception instead of returning 0 + return 0L; + } + else if(stopTime == null){ + return System.nanoTime() - startTime; + } + else{ + return stopTime - startTime; + } + } + //An enum that helps keep track of how many times the time has been reduced in the getStr function + private enum TIME_RESOLUTION{ NANOSECOND, MICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR, ERROR } + //Simulates starting a stopwatch by saving the time + public void start(){ + //Get the time as close to calling the function as possible + startTime = System.nanoTime(); + //Make sure the stop time is reset to 0 + stopTime = null; + } + //SImulates stoping a stopwatch by saving the time + public void stop(){ + //Set the stopTime as close to call time as possible + stopTime = System.nanoTime(); + //If the startTime has not been set then reset stopTime + if(startTime == null){ + stopTime = null; + } + } + //Resets all variables in the stopwatch + public void reset(){ + //Make sure all variables are reset correctly + startTime = null; + stopTime = null; + } + //Returns the time in nanoseconds + public double getNano(){ + return getTime().doubleValue(); + } + //Returns the time in microseconds + public double getMicro(){ + return getTime().doubleValue() / 1000D; + } + //Returns the time in milliseconds + public double getMilli(){ + return getTime().doubleValue() / 1000000D; + } + //Returns the time in seconds + public double getSecond(){ + return getTime().doubleValue() / 1000000000D; + } + //Returns the time in minutes + public double getMinute(){ + return getTime().doubleValue() / 60000000000D; + } + //Returns the time in hours + public double getHour(){ + return getTime().doubleValue() / 3600000000000D; + } + //Returns the time as a string at the 'best' resolution. (Goal is xxx.xxx) + public String getStr(){ + //Get the current duration from time + Double duration = getTime().doubleValue(); + //Reduce the number to the appropriate number of digits. (xxx.x). This loop works down to seconds + TIME_RESOLUTION resolution; + for(resolution = TIME_RESOLUTION.NANOSECOND;(resolution.ordinal() < TIME_RESOLUTION.SECOND.ordinal()) && (duration >= 1000);resolution = TIME_RESOLUTION.values()[resolution.ordinal() + 1]){ + duration /= 1000; + } + //Check if the duration needs reduced to minutes + if(duration >= 1000){ + //Reduce to minutes + duration /= 60; + resolution = TIME_RESOLUTION.values()[resolution.ordinal() + 1]; + } + //Check if the duration needs reduced to hours + if(duration >= 1000){ + //Reduce to hours + duration /= 60; + resolution = TIME_RESOLUTION.values()[resolution.ordinal() + 1]; + } + + //Turn the number into a string + Double durationFraction = ((duration % 1) * 100); + String time = String.format("% 3d.%03d", duration.intValue(), durationFraction.intValue()); + + //Tack on the appropriate suffix for resolution + switch(resolution){ + case NANOSECOND: time += " nanoseconds"; break; + case MICROSECOND: time += " microseconds"; break; + case MILLISECOND: time += " milliseconds"; break; + case SECOND: time += " seconds"; break; + case MINUTE: time += " minutes"; break; + case HOUR: time += " hours"; break; + case ERROR: + default: time = "There was an error computing the time"; break; ///This should throw an error instead + } + //Return the string + return time; + } +} diff --git a/testStopwatch.java b/testStopwatch.java new file mode 100644 index 0000000..d715cf1 --- /dev/null +++ b/testStopwatch.java @@ -0,0 +1,67 @@ +//Java/JavaClasses/testStopwatch.java +//Matthew Ellison +// Created: 03-01-19 +//Modified: 03-01-19 +//This class is used to test that stopwatch does what it is supposed to do (approximately) + + +import mattrixwv.Stopwatch; + + +public class testStopwatch{ + private static final int NUM_TO_RUN = 100000; + public static void main(String[] argv){ + Boolean failed = false; //A flag to determine if all tests were passed + //Print the test begin message + System.out.println("Begin test"); + //Describe which test is being performed + System.out.println("Starting loop"); + //Start the timer + Stopwatch timer = new Stopwatch(); + timer.start(); + for(int cnt = 0;cnt < NUM_TO_RUN;++cnt){ + System.out.print(cnt); + } + //Stop the timer + timer.stop(); + System.out.println("\nLoop completed"); + + //Check that the different resolutions work out correctly + System.out.println("Checking that resolutions line up correctly"); + Double nano = timer.getNano(); + if(timer.getMicro() != (nano.doubleValue() / 1000D)){ + System.out.println("Error on microsecond resolution"); + failed = true; + } + else if(timer.getMilli() != (nano.doubleValue() / 1000000D)){ + System.out.println("Error on millisecond resolution"); + failed = true; + } + else if(timer.getSecond() != (nano.doubleValue() / 1000000000D)){ + System.out.println("Error on second resolution"); + failed = true; + } + else if(timer.getMinute() != (nano.doubleValue() / 60000000000D)){ + System.out.println("Error on minute resolution"); + failed = true; + } + else if(timer.getHour() != (nano.doubleValue() / 3600000000000D)){ + System.out.println("Error on hour resolution"); + failed = true; + } + else{ + System.out.println("All time resolution tests completed successfully"); + } + System.out.println("Completed resolution checking"); + + //Print the results + System.out.printf("The timer results in: %s\n", timer.getStr()); + + if(!failed){ + System.out.println("All tests completed successfully"); + } + else{ + System.out.println("Test failed. Try again!"); + } + } +} \ No newline at end of file