diff --git a/Stopwatch.h b/Stopwatch.h new file mode 100644 index 0000000..dbe0429 --- /dev/null +++ b/Stopwatch.h @@ -0,0 +1,137 @@ +//Programs/C/myHelpers/Stopwatch.h +//Matthew Ellison +// Created: 03-08-19 +//Modified: 03-08-19 +//This struct helps by using a struct to simplify timing a program in C + + +#include +#include +#include +#include +#include + + +struct Stopwatch{ + uint64_t startTime; + uint64_t stopTime; +}; + +void init(struct Stopwatch* timer){ + //You need everything to start at 0 + timer->startTime = timer->stopTime = (uint64_t)0; +} + +void reset(struct Stopwatch* timer){ + //Reseting is basically the same as initializing + init(timer); +} + +void start(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){ + //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 + if(timer->startTime == (uint64_t)0){ + timer->stopTime = (uint64_t)0; + } +} + +uint64_t getTime(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)){ + return (timer->stopTime - timer->startTime); + } + //If the stopwatch has been started but not stopped return the current time as the stop time. Simulates looking at it while it's still running + else if((timer->startTime != (uint64_t)0) && (timer->stopTime == (uint64_t)0)){ + return (tempTime - timer->startTime); + } + //If the stopwatch was never started return 0 + else{ + return (uint64_t)0; + } +} + +//Gets the time from a stopwatch at nanosecond resolution +uint64_t getNano(struct Stopwatch* timer){ + return ((getTime(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); +} + +//Gets the time from a stopwatch at millisecond resolution +double getMilli(struct Stopwatch* timer){ + return ((getTime(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); +} + +//Gets the time from a stopwatch at minute resolution +double getMinute(struct Stopwatch* timer){ + return ((getTime(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); +} + +//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* 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); + 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); + 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); + 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); + 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); + 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); + 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); + } + + return num; +} \ No newline at end of file diff --git a/testStopwatch.c b/testStopwatch.c new file mode 100644 index 0000000..b591d85 --- /dev/null +++ b/testStopwatch.c @@ -0,0 +1,42 @@ +//Programs/C/myHelpers/testStopwatch.c +//Matthew Ellison +// Created: 03-08-19 +//Modified: 03-08-19 +//This program is a simple test for the stopwatch struct and accompanying functions + + +#include +#include +#include "Stopwatch.h" + +const int NUM_OF_RUNS = 10000; + + +int main(){ + struct Stopwatch timer; + init(&timer); + start(&timer); + for(int cnt = 0;cnt < NUM_OF_RUNS;++cnt){ + printf("%d\n", cnt); + } + stop(&timer); + printf("It took %d nanoseconds to run this program\n", getNano(&timer)); + printf("It took %f microseconds to run this program\n", getMicro(&timer)); + printf("It took %f milliseconds to run this program\n", getMilli(&timer)); + printf("It took %f seconds to run this program\n", getSecond(&timer)); + printf("It took %f minutes to run this program\n", getMinute(&timer)); + printf("It took %f hours to run this program\n", getHour(&timer)); + printf("It took %s to run this program\n", getStr(&timer)); + + return 0; +} + +/* Results: +It took 1289000000 nanoseconds to run this program +It took 1289000.000000 microseconds to run this program +It took 1289.000000 milliseconds to run this program +It took 1.289000 seconds to run this program +It took 0.021483 minutes to run this program +It took 0.003581 hours to run this program +It took 1.288 seconds to run this program +*/