Here's some explanation for accuracy
and neg_accuracy
:
It's meant to keep all the values in terms of what the user specified he wanted (how many decimal digits deep they want to go). accuracy
is meant to cut the huge number returned by GetTickCount()
to size, and neg_accuracy
actually turns it into a decimal by dividing.
I'm just hoping to get some criticism on efficiency and be notified of any better ways to do things.
// -----------------------------------INSTRUCTIONS-----------------------------------
// create a timer class object with with this function: ___Stopwatch PLACE_NAME_HERE( int NumberOfDecimals, string Command);___
// Stopwatch PLACE_NAME_HERE( int NumberOfDecimals,
//put how many decimal integers deep you want tracked time to be (
// 1 decimal = 0.0, inserting an integer with value 2 here = 0.00)
// string Command); // putting the command "Start" here makes it start timing right away
// member functions include: Start() {starts the timer}
// Restart() {restarts the timer but doesn't clear saved Times}
// TimeElapsed() {returns the time elapsed since the Start() function was called}
// Wait( long float TIME_TO_WAIT) {doesn't return until time specified is over}
// SaveTime() {Saves the current time elapsed into a vector which can be accesed with:}
// GetTime( int TIME_TO_GET ) {returns the specified time; if you specify 1 you get the first time you saved
// otherwise the funciton returns 0 if there is no time saved at specified location}
// ClearTime() {Clears all saved times in the vector}
// Call these functions by simply typing "STOPWATCH_NAME.A_FUNCTION();" and filling in the capitalized variables
// Windows stuff
#include <Windows.h>
// STL stuff
#include <string>
#include <vector>
using namespace std;
class Stopwatch
{
protected:
int accuracy, neg_accuracy;
unsigned long int initial_ilu;
unsigned long int current_ilu;
long float output_fl;
vector<long float> times;
public:
Stopwatch(int DecNumb, string command = "void") { // default command
if (DecNumb>3) DecNumb = 3;
// I made here before DecNumb gets changed
int I = 3 - DecNumb;
// so accuracy*10 isn't 0
for(accuracy = 1;I>0;I--)
accuracy = accuracy*10;
for(neg_accuracy = 1;DecNumb>0;DecNumb--)
neg_accuracy = neg_accuracy*10;
if(command == "start" ||command == "Start") Start();};
void Start(){initial_ilu = GetTickCount()/accuracy;};
void Restart(){initial_ilu = GetTickCount()/accuracy;};
long float ElapsedTime()
{
current_ilu = GetTickCount()/accuracy;
output_fl = ((long float)current_ilu - (long float)initial_ilu)/(long float)neg_accuracy;
return output_fl;
};
void Wait(long float seconds)
{
for(unsigned long int waitTime = GetTickCount() + (seconds*1000);
waitTime > GetTickCount();) {}
// stay stuck in for loop until time specified is up
};
void SaveTime(){times.push_back(ElapsedTime());};
void ClearTime(){times.clear();times.resize(0);}; // WILL THIS WORK? (untested)
long float GetTime(int location){int test = times.size();
if(times.size() >= location) return times[location-1];
};
};