mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-06 23:13:57 -05:00
122 lines
3.9 KiB
Java
122 lines
3.9 KiB
Java
//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) * 1000);
|
|
String time = String.format("%d.%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;
|
|
}
|
|
}
|