Files
CClasses/Stopwatch.h

161 lines
5.7 KiB
C

//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
/*
Copyright (C) 2019 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef STOPWATCH_H
#define STOPWATCH_H
#include <time.h>
#include <inttypes.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
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;
}
#endif //STOPWATCH_H