0

我需要执行 64 秒的操作。每 8 秒我需要执行另一个操作。因为我需要一个极端的时间精度,所以我正在使用QueryPerformanceFrequencyQueryPerformanceCounter. 如果我想执行 64 秒或 8 秒的操作,我该如何用 type 表示这些秒数unsigned long long int?当我不需要这种精度时,我做了类似的事情:

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

现在我正在这样做。

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...
        }
    }
}

显然在这种情况下不起作用,因为TIME_TO_RUNis anintcurrentTime-startTime返回 an unsigned long long int。如果声明TIME_TO_RUN

const unsigned long long int TIME_TO_RUN = 8;

不起作用。如何用无符号长长表示八秒。

感谢您的回答。

编辑:我无法解决这个问题。

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

在这个例子中,我需要运行 96 秒。每 12 秒我需要更改目标。在这 12 秒内,我需要 4 秒的休息时间和 1 秒的时间等待一些变化。最后 7 秒我需要执行一些操作。
问题是应用程序没有运行 96 秒,而是在几秒钟后结束。当应用程序终止时,我没有收到任何错误。
在我使用过这个的另一个应用程序中if (currentTime.QuadPart-startTime.QuadPart <timeToRun.QuadPart),我可以运行该应用程序所需的时间。

编辑2:
计时器有任何问题。问题是由于我使用的索引比数组的长度长。

4

1 回答 1

1

你期待frequency.QuadPart/1000000做什么?

我想你正在寻找这样的东西?

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.
}
于 2013-04-20T15:44:29.373 回答