Updated to give better names

This commit is contained in:
2019-03-11 11:54:33 -04:00
parent 6c36ade440
commit 4367c34874
2 changed files with 63 additions and 48 deletions

View File

@@ -37,24 +37,24 @@ struct Stopwatch{
uint64_t stopTime;
};
void init(struct Stopwatch* timer){
void initStopwatch(struct Stopwatch* timer){
//You need everything to start at 0
timer->startTime = timer->stopTime = (uint64_t)0;
}
void reset(struct Stopwatch* timer){
void resetStopwatch(struct Stopwatch* timer){
//Reseting is basically the same as initializing
init(timer);
initStopwatch(timer);
}
void start(struct Stopwatch* timer){
void startStopwatch(struct Stopwatch* timer){
//If you just started the stopwatch make sure the stop time is 0
timer->stopTime = (uint64_t)0;
//Record the start time
timer->startTime = clock();
}
void stop(struct Stopwatch* timer){
void stopStopwatch(struct Stopwatch* timer){
//Get the stop time as close to calling as possible
timer->stopTime = clock();
//If the stopwatch was never started pretend like you never recorded anything in stop
@@ -63,7 +63,7 @@ void stop(struct Stopwatch* timer){
}
}
uint64_t getTime(struct Stopwatch* timer){
uint64_t getTimeStopwatch(struct Stopwatch* timer){
uint64_t tempTime = clock();
//If everything is performing normally return what is expected
if((timer->stopTime != (uint64_t)0) && (timer->startTime != (uint64_t)0)){
@@ -80,74 +80,74 @@ uint64_t getTime(struct Stopwatch* timer){
}
//Gets the time from a stopwatch at nanosecond resolution
uint64_t getNano(struct Stopwatch* timer){
return ((getTime(timer) / (double)CLOCKS_PER_SEC) * 1000000000);
uint64_t getNanoStopwatch(struct Stopwatch* timer){
return ((getTimeStopwatch(timer) / (double)CLOCKS_PER_SEC) * 1000000000);
}
//Gets the time from a stopwatch at microsecond resolution
double getMicro(struct Stopwatch* timer){
return ((getTime(timer) / (double)CLOCKS_PER_SEC) * 1000000);
double getMicroStopwatch(struct Stopwatch* timer){
return ((getTimeStopwatch(timer) / (double)CLOCKS_PER_SEC) * 1000000);
}
//Gets the time from a stopwatch at millisecond resolution
double getMilli(struct Stopwatch* timer){
return ((getTime(timer) / (double)CLOCKS_PER_SEC) * 1000);
double getMilliStopwatch(struct Stopwatch* timer){
return ((getTimeStopwatch(timer) / (double)CLOCKS_PER_SEC) * 1000);
}
//Gets the time from a stopwatch at second resolution
double getSecond(struct Stopwatch* timer){
return (getTime(timer) / (double)CLOCKS_PER_SEC);
double getSecondStopwatch(struct Stopwatch* timer){
return (getTimeStopwatch(timer) / (double)CLOCKS_PER_SEC);
}
//Gets the time from a stopwatch at minute resolution
double getMinute(struct Stopwatch* timer){
return ((getTime(timer) / (double)CLOCKS_PER_SEC) / 60);
double getMinuteStopwatch(struct Stopwatch* timer){
return ((getTimeStopwatch(timer) / (double)CLOCKS_PER_SEC) / 60);
}
//Gets the time from a stopwatch at hour resolution
double getHour(struct Stopwatch* timer){
return ((getTime(timer) / (double)CLOCKS_PER_SEC) / 360);
double getHourStopwatch(struct Stopwatch* timer){
return ((getTimeStopwatch(timer) / (double)CLOCKS_PER_SEC) / 360);
}
//Gets the time from a stopwatch at the "Best" resolution. The goal is XXX.XXX [Resolution]
//While it would be possible to use this function to get the time while still running it is not recomended because of the time it takes to return the string
char* getStr(struct Stopwatch* timer){
char* getStrStopwatch(struct Stopwatch* timer){
char* num = (char*)malloc(21 * sizeof(char)); //Holds the string that will be created in the end
double dur; //Holds the duration the stopwatch has run
//Decide what the best way to get time is. Looking for a XXX.XXX format
if(getNano(timer) < 1000){
dur = getNano(timer);
if(getNanoStopwatch(timer) < 1000){
dur = getNanoStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration
uint64_t fractDur = (fmod(dur, 1.0) * 1000); //Holds the integer representation of the fractional part of the duration
sprintf(num, "%d.%03d nanoseconds", intDur, fractDur);
}
else if(getMicro(timer) < 1000){
dur = getMicro(timer);
else if(getMicroStopwatch(timer) < 1000){
dur = getMicroStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration
uint64_t fractDur = (fmod(dur, 1.0) * 1000); //Holds the integer representation of the fractional part of the duration
sprintf(num, "%d.%03d microseconds", intDur, fractDur);
}
else if(getMilli(timer) < 1000){
dur = getMilli(timer);
else if(getMilliStopwatch(timer) < 1000){
dur = getMilliStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration
uint64_t fractDur = (fmod(dur, 1.0) * 1000); //Holds the integer representation of the fractional part of the duration
sprintf(num, "%d.%03d milliseconds", intDur, fractDur);
}
else if(getSecond(timer) < 1000){
dur = getSecond(timer);
else if(getSecondStopwatch(timer) < 1000){
dur = getSecondStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration
uint64_t fractDur = (fmod(dur, 1.0) * 1000); //Holds the integer representation of the fractional part of the duration
sprintf(num, "%d.%03d seconds", intDur, fractDur);
}
else if(getMinute(timer) < 1000){
dur = getMinute(timer);
else if(getMinuteStopwatch(timer) < 1000){
dur = getMinuteStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration
uint64_t fractDur = (fmod(dur, 1.0) * 1000); //Holds the integer representation of the fractional part of the duration
sprintf(num, "%d.%03d minutes", intDur, fractDur);
}
else{
dur = getHour(timer);
dur = getHourStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration
uint64_t fractDur = (fmod(dur, 1.0) * 1000); //Holds the integer representation of the fractional part of the duration
sprintf(num, "%d.%03d hours", intDur, fractDur);