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; uint64_t stopTime;
}; };
void init(struct Stopwatch* timer){ void initStopwatch(struct Stopwatch* timer){
//You need everything to start at 0 //You need everything to start at 0
timer->startTime = timer->stopTime = (uint64_t)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 //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 //If you just started the stopwatch make sure the stop time is 0
timer->stopTime = (uint64_t)0; timer->stopTime = (uint64_t)0;
//Record the start time //Record the start time
timer->startTime = clock(); timer->startTime = clock();
} }
void stop(struct Stopwatch* timer){ void stopStopwatch(struct Stopwatch* timer){
//Get the stop time as close to calling as possible //Get the stop time as close to calling as possible
timer->stopTime = clock(); timer->stopTime = clock();
//If the stopwatch was never started pretend like you never recorded anything in stop //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(); uint64_t tempTime = clock();
//If everything is performing normally return what is expected //If everything is performing normally return what is expected
if((timer->stopTime != (uint64_t)0) && (timer->startTime != (uint64_t)0)){ 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 //Gets the time from a stopwatch at nanosecond resolution
uint64_t getNano(struct Stopwatch* timer){ uint64_t getNanoStopwatch(struct Stopwatch* timer){
return ((getTime(timer) / (double)CLOCKS_PER_SEC) * 1000000000); return ((getTimeStopwatch(timer) / (double)CLOCKS_PER_SEC) * 1000000000);
} }
//Gets the time from a stopwatch at microsecond resolution //Gets the time from a stopwatch at microsecond resolution
double getMicro(struct Stopwatch* timer){ double getMicroStopwatch(struct Stopwatch* timer){
return ((getTime(timer) / (double)CLOCKS_PER_SEC) * 1000000); return ((getTimeStopwatch(timer) / (double)CLOCKS_PER_SEC) * 1000000);
} }
//Gets the time from a stopwatch at millisecond resolution //Gets the time from a stopwatch at millisecond resolution
double getMilli(struct Stopwatch* timer){ double getMilliStopwatch(struct Stopwatch* timer){
return ((getTime(timer) / (double)CLOCKS_PER_SEC) * 1000); return ((getTimeStopwatch(timer) / (double)CLOCKS_PER_SEC) * 1000);
} }
//Gets the time from a stopwatch at second resolution //Gets the time from a stopwatch at second resolution
double getSecond(struct Stopwatch* timer){ double getSecondStopwatch(struct Stopwatch* timer){
return (getTime(timer) / (double)CLOCKS_PER_SEC); return (getTimeStopwatch(timer) / (double)CLOCKS_PER_SEC);
} }
//Gets the time from a stopwatch at minute resolution //Gets the time from a stopwatch at minute resolution
double getMinute(struct Stopwatch* timer){ double getMinuteStopwatch(struct Stopwatch* timer){
return ((getTime(timer) / (double)CLOCKS_PER_SEC) / 60); return ((getTimeStopwatch(timer) / (double)CLOCKS_PER_SEC) / 60);
} }
//Gets the time from a stopwatch at hour resolution //Gets the time from a stopwatch at hour resolution
double getHour(struct Stopwatch* timer){ double getHourStopwatch(struct Stopwatch* timer){
return ((getTime(timer) / (double)CLOCKS_PER_SEC) / 360); return ((getTimeStopwatch(timer) / (double)CLOCKS_PER_SEC) / 360);
} }
//Gets the time from a stopwatch at the "Best" resolution. The goal is XXX.XXX [Resolution] //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 //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 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 double dur; //Holds the duration the stopwatch has run
//Decide what the best way to get time is. Looking for a XXX.XXX format //Decide what the best way to get time is. Looking for a XXX.XXX format
if(getNano(timer) < 1000){ if(getNanoStopwatch(timer) < 1000){
dur = getNano(timer); dur = getNanoStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration 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 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); sprintf(num, "%d.%03d nanoseconds", intDur, fractDur);
} }
else if(getMicro(timer) < 1000){ else if(getMicroStopwatch(timer) < 1000){
dur = getMicro(timer); dur = getMicroStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration 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 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); sprintf(num, "%d.%03d microseconds", intDur, fractDur);
} }
else if(getMilli(timer) < 1000){ else if(getMilliStopwatch(timer) < 1000){
dur = getMilli(timer); dur = getMilliStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration 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 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); sprintf(num, "%d.%03d milliseconds", intDur, fractDur);
} }
else if(getSecond(timer) < 1000){ else if(getSecondStopwatch(timer) < 1000){
dur = getSecond(timer); dur = getSecondStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration 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 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); sprintf(num, "%d.%03d seconds", intDur, fractDur);
} }
else if(getMinute(timer) < 1000){ else if(getMinuteStopwatch(timer) < 1000){
dur = getMinute(timer); dur = getMinuteStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration 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 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); sprintf(num, "%d.%03d minutes", intDur, fractDur);
} }
else{ else{
dur = getHour(timer); dur = getHourStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration 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 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); sprintf(num, "%d.%03d hours", intDur, fractDur);

View File

@@ -29,30 +29,45 @@ const int NUM_OF_RUNS = 10000;
int main(){ int main(){
struct Stopwatch timer; struct Stopwatch timer; //Create the stopwatch
init(&timer); initStopwatch(&timer); //Initialize the stopwatch
start(&timer);
//Start the stopwatch counting
startStopwatch(&timer);
//Perform some calculation
for(int cnt = 0;cnt < NUM_OF_RUNS;++cnt){ for(int cnt = 0;cnt < NUM_OF_RUNS;++cnt){
printf("%d\n", cnt); printf("%d\n", cnt);
} }
stop(&timer);
printf("It took %d nanoseconds to run this program\n", getNano(&timer)); //Stop the stopwatch counting
printf("It took %f microseconds to run this program\n", getMicro(&timer)); stopStopwatch(&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)); //Get a string about the time
printf("It took %f minutes to run this program\n", getMinute(&timer)); char* timerStr = getStrStopwatch(&timer);
printf("It took %f hours to run this program\n", getHour(&timer));
printf("It took %s to run this program\n", getStr(&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; return 0;
} }
/* Results: /* Results:
It took 1289000000 nanoseconds to run this program It took 1938000000 nanoseconds to run this program
It took 1289000.000000 microseconds to run this program It took 1938000.000000 microseconds to run this program
It took 1289.000000 milliseconds to run this program It took 1938.000000 milliseconds to run this program
It took 1.289000 seconds to run this program It took 1.938000 seconds to run this program
It took 0.021483 minutes to run this program It took 0.032300 minutes to run this program
It took 0.003581 hours to run this program It took 0.005383 hours to run this program
It took 1.288 seconds to run this program It took 1.938 seconds to run this program
*/ */