简体   繁体   中英

How can I represent a certain number of seconds (minutes) with the type unsigned long long int in C++

I need to perform an operation for 64 seconds. Every 8 seconds I need to perform another operation. Since I need an extreme temporal precision I am using QueryPerformanceFrequency and QueryPerformanceCounter . If I wanted to execute an operation for 64 seconds, or 8 seconds, how can I represent these seconds with the type unsigned long long int ? when I did not need this precision, I did something like that:

const int TIME_TO_RUN= 64;
time_t startTime = 0;
...
time (& startTime);
while (difftime (time (NULL), startTime) <TIME_TO_RUN) {
    do something
}

Now I am doing that.

typedef unsigned long long int accurateTime;

//GetCurrentTime void (* accurateTime time) {
  void GetCurrentTime(accurateTime *time) {

    LARGE_INTEGER frequency, currentCount;
    QueryPerformanceFrequency (& frequency); / / Units is (counts / sec)
    QueryPerformanceCounter (& currentCount);
    * time = (accurateTime) (currentCount.QuadPart / (frequency.QuadPart/1000000));
}

int main () {
    accurateTime startTime = 0;
    accurateTime CurrentTime = 0;
    ...
    GetCurrentTime (& startTime);
    while (true) {
        GetCurrentTime (& currentTime);
        if (currentTime-startTime <TIME_TO_RUN) {
            do something...
        }
    }
}

obviously in this case does not work because TIME_TO_RUN is an int and currentTime-startTime returns an unsigned long long int . If declare TIME_TO_RUN as

const unsigned long long int TIME_TO_RUN = 8;

does not work. How can I represent eight seconds with an unsigned long long.

Thanks for answers.

EDIT: I can not solve this problem.

#define NUMBER_STIMULI 8
#define DURATION_STIMULI 7
#define TIME_WAIT 1
#define SELECT_STIMULI_TIME 4
#define TIME_TO_RUN NUMBER_STIMULI*(DURATION_STIMULI + TIME_WAIT + SELECT_STIMULI_TIME)

...

LARGE_INTEGER startTime, currentTime, timeToRun, waitTime, lastWaitTime, stimuliTime, lastStimuliTime, endTime, frequency, selectStimuliTime, lastSelectStimulitiTime;


QueryPerformanceFrequency(&frequency);

QueryPerformanceFrequency(&waitTime);
waitTime.QuadPart*=TIME_WAIT;

QueryPerformanceFrequency(&selectStimuliTime);
selectStimuliTime.QuadPart*=SELECT_STIMULI_TIME;

QueryPerformanceFrequency(&stimuliTime);
stimuliTime.QuadPart*=(TIME_WAIT+DURATION_STIMULI+SELECT_STIMULI_TIME);

QueryPerformanceFrequency(&timeToRun);
timeToRun.QuadPart*=TIME_TO_RUN;

...

QueryPerformanceCounter(&startTime);

for(;;) {
    QueryPerformanceCounter(&currentTime);
    if(currentTime.QuadPart-startTime.QuadPart<timeToRun.QuadPart) { //run for 96 seconds
        if((currentTime.QuadPart-lastStimuliTime.QuadPart>=stimuliTime.QuadPart) { each 12 seconds
            QueryPerformanceCounter(&lastStimuliTime);
            QueryPerformanceCounter(&lastSelectStimulitiTime);
            }
        else { //for 12 seconds
            if((currentTime.QuadPart-lastSelectStimulitiTime.QuadPart<selectStimuliTime.QuadPart)) { //wait for 4 seconds
                QueryPerformanceCounter(&lastWaitTime);
            }
            else {
                if(currentTime.QuadPart-lastWaitTime.QuadPart<waitTime.QuadPart) { //wait for 1 second
                }
                else { for 7 seconds
                    make something;
                }
            }
        }
    }
}

I need, in this example, to run for 96 seconds. Each 12 seconds I need to change the target. In this 12 seconds, I need for 4 seconds to give a break and 1 second to wait some changes. The last 7 seconds I need to perform some operations.
The problem is that the application does not run for 96 seconds, but it ends after a few seconds. When the application terminates I do not receive any error.
In another application where I have used this if (currentTime.QuadPart-startTime.QuadPart <timeToRun.QuadPart) , I can run the application for the desired time.

EDIT 2:
There are any problems with timer. The problem was due to the fact I used an index that was longer than the length of the array.

What are you expecting frequency.QuadPart/1000000 to do?

I think you're looking for something like this?

LARGE_INTEGER startTime, endTime, freq;

QueryPerformanceFrequency(&freq); // freq holds the number of ticks in 1 second.
freq.QuadPart *= 8; // and now in 8 seconds.

QueryPerformanceCounter(&startTime);

for(;;)
{
    QueryPerformanceCounter(&endTime);

    if((endTime.QuadPart - startTime.QuadPart) >= freq.QuadPart)
    {
        // 8 seconds have passed.
        break;
    }

    // do something.
}

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