7
\$\begingroup\$

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];
                                        };

    };
\$\endgroup\$
5
\$\begingroup\$

Why are you using strings to command the class? They may be fast enough for your needs (since you're only using tick) but they are generally slow, and if you were to extend the class to support faster timing it could throw off your results. Consider using an enum or define to command the class:

In your header, define the following:

enum{
STOPTIMER = 0,
STARTTIMER = 1
};

Then modify your class:

Stopwatch(..., int command = STOPTIMER) { 
...
if(command == STARTTIMER) Start();};

I'm not a big fan of some of your coding style, but at least consider keeping it consistent. If you're going to put braces at the end of the line elsewhere, do it for the class declaration as well. Consider indenting the code in the loops so the looping structure is obvious.

Also, note that GetTickCount() is not very accurate - you should consider making certain that your class advertises just how accurate it can be, since you give them the ability to choose an accuracy. They may choose one that the class really can't support. Not only is it accurate to a millisecond (only 3 decimal places) it's only updated 64 times a second, so it's really only accurate to just under 2 decimal places. See http://en.wikipedia.org/wiki/GetTickCount for more info. There are other system timing functions that will give you very accurate mS and uS timing.

\$\endgroup\$
  • \$\begingroup\$ Can you suggest any other system timing functions i can use to easily replace GetTickCount() ? Thanks for the answer too! \$\endgroup\$ – Griffin May 14 '11 at 0:44
4
\$\begingroup\$

The most accurate timing mechanism on Windows is QueryPerformanceCounter. If you use this together with QueryPerformanceCounterFrequency you can create a microsecond accurate timer.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.