简体   繁体   中英

My Simple Game's Timer is Very Off… C++

So this is the same game from an earlier post (Link is Here ). The title of that post was "C++ Clock Not Working" and I have fixed the clock, "Hooray!". Now the process I am using to calculate the duration seems to be broken. I always get 'x.y'e-05 where the time should be in seconds yet the timer stops well before 'x.y'e-05. Also does the 05 mean it is in base 8? If so why???

I'm sure there is a very simple solution I am just missing. Any Answers would be appreciated...

Code:

          do {
                //Procedere For Each Round

                clock_t start;

                //Clock
                start = clock();

                cout<<"The number is:  "<< actualNumber<<endl;
                getline(cin, numberGuessed);
                intNumberGuessed = stoi(numberGuessed);

                clock_t finish;
                finish = clock();

                double elapsed = (double)(finish-start);


                duration = elapsed/CLOCKS_PER_SEC;

                cout<<"The Duration Is:  "<< duration<<endl; //FOR TESTING...

                //Test User's input

               //timeForInput is chosen earlier and is either 1,2,or 5.
                if((intNumberGuessed == actualNumber) && (duration <= (timeForInput/100000))){
                    score += 1;
                    gameOver = 0;

                } else if ((intNumberGuessed != actualNumber) || (duration >= (timeForInput/100000))) {
                    gameOver = 1;
                }

                //Reset Number
               actualNumber = rand() % 4 + 1;

               //Reset Clock


            } while (gameOver != 1);
        }

        cout<<"You Failed!"<<endl;
        sleep(1);
        cout<<"Your Score Was:  "<<score<<endl;

        return 0;

The problem is that clock() does not do what you think it does. You're under the (not unreasonable, given the function's name) impression that clock() returns a value representing wall-clock time, but what clock() is actually returning is a tally of the total time when your program has been actively executing on a CPU core . That is to say, any time that your program spent "asleep" (eg when it was waiting for input) does not cause the value returned by clock() to increase. As the man page says:

The clock() function returns an approximation of processor time used by the program.

That explains why you are measuring a very small number (x.ye-05 is scientific notation, iexy * 10^-5 seconds) -- your program spends very little time executing because during most of the interval you measure your program is asleep, blocked inside getline() waiting for the user to type something.

So clock() isn't going to work for your purpose. You'd be better off calling eg gettimeofday() and converting its results into a value in microseconds, like this:

// Returns a "current wall clock time" value, in microseconds
unsigned long long GetWallClockTimeInMicroseconds()
{
   struct timeval tv;
   gettimeofday(&tv, NULL);
   return ((unsigned long long)tv.sec)*1000000 + tv.tv_usec;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM