【问题标题】:what is the maximum delay or jitter in using Queryperformance counter?使用 Queryperformancecounter 的最大延迟和抖动是多少?
【发布时间】:2013-11-12 21:02:52
【问题描述】:
#include "TIMER1.h"
#include "MAIN.h"  
typedef unsigned _int64 uint64;

void TASK1()
{
uint64 freq, start, end, diff;
//unsigned int milliseconds;

QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
QueryPerformanceCounter((LARGE_INTEGER*)&start);

// code to measure
printf("hi\n");
printf("hi1\n");
printf("hi2\n");

QueryPerformanceCounter((LARGE_INTEGER*)&end);
diff = (((end - start) * 1000) / freq);
//milliseconds = (unsigned int)(diff & 0xffffffff);
printf("It took %u ms\n",diff);

}

我从 main 中多次调用 void TASK1() 函数并分析打印所需的时间,如上述代码(即 hi、hi1、hi2)。我正在计算打印 hi、hi1、hi2 的开始时间和结束时间之间的时间差。 我的问题:为什么我的输出延迟并且无法完全按预期打印。

输出错误: 你好 嗨1 嗨2 花了0毫秒

嗨 嗨1 嗨2 花了1毫秒

有时在输出中: 嗨1 嗨2 花了2毫秒

这是什么原因?? 如何将上述代码从毫秒更改为微秒??

【问题讨论】:

  • 很难说,但是 QueryPerformanceCounter 的问题在于,在使用节能功能的现代处理器上,它的准确性可能会下降。无论如何,这里有一个有趣的帖子:stackoverflow.com/questions/7287663/… 它可能会有所帮助。在用户级别,您还必须考虑从收到时间到您的应用报告时间之间的延迟。
  • 没有最大抖动,这完全取决于您的操作系统当时正在运行哪些后台任务。你真的需要帮助将毫秒转换为微秒吗?您已经从秒转换为毫秒。
  • 转换为微秒是否正确 diff = (((end - start) * 1000000) / freq); ??
  • 非常感谢您的回复:Mark Ransom,Jekyll
  • 如果我如上所述转换为微秒,那么输出中会有更多的抖动。我正在使用 Windows 操作系统。

标签: c windows timer performancecounter


【解决方案1】:
  1. 不同的延迟。 ...这是什么原因?

    printf() 与其他进程同步,因此所需时间可能会有所不同。

  2. ...如何将上面的代码从毫秒改成微秒?

    diff = (((end - start) * 1000000) / freq);
    printf("It took %u us\n",diff);
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多