diff --git a/Stopwatch.h b/Stopwatch.h index 2be2f41..66fd329 100644 --- a/Stopwatch.h +++ b/Stopwatch.h @@ -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); diff --git a/testStopwatch.c b/testStopwatch.c index 98bc4ea..d77bb0d 100644 --- a/testStopwatch.c +++ b/testStopwatch.c @@ -29,30 +29,45 @@ const int NUM_OF_RUNS = 10000; int main(){ - struct Stopwatch timer; - init(&timer); - start(&timer); + struct Stopwatch timer; //Create the stopwatch + initStopwatch(&timer); //Initialize the stopwatch + + //Start the stopwatch counting + startStopwatch(&timer); + + //Perform some calculation 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)); + + //Stop the stopwatch counting + stopStopwatch(&timer); + + //Get a string about the time + char* timerStr = getStrStopwatch(&timer); + + //Print the results + printf("It took %u nanoseconds to run this program\n", getNanoStopwatch(&timer)); + printf("It took %f microseconds to run this program\n", getMicroStopwatch(&timer)); + printf("It took %f milliseconds to run this program\n", getMilliStopwatch(&timer)); + printf("It took %f seconds to run this program\n", getSecondStopwatch(&timer)); + printf("It took %f minutes to run this program\n", getMinuteStopwatch(&timer)); + printf("It took %f hours to run this program\n", getHourStopwatch(&timer)); + printf("It took %s to run this program\n", timerStr); + + //Free the memory used in the string + free(timerStr); + timerStr = NULL; 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 +It took 1938000000 nanoseconds to run this program +It took 1938000.000000 microseconds to run this program +It took 1938.000000 milliseconds to run this program +It took 1.938000 seconds to run this program +It took 0.032300 minutes to run this program +It took 0.005383 hours to run this program +It took 1.938 seconds to run this program */