操作系统:win2000
工具:  vc6.0......
for (;;)
{
  start = timeGetTime();  while ((end = timeGetTime()-start)<4)
  {
     if (NULL!=PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
     {
        TranslateMessage(&msg);
        DispatchMessage(&msg);     
     }
  }
  end = timeGetTime();
  end = start - end;
}我用多媒体时钟,限制这段程序的时间。逻辑上来讲应该是在end大于4的时候退出,但是我多次调试,start设置一个断点, 第二个end处设置一个断点。end最后的结果不是15就是16。我想原因是不是系统问题呢?
下面这段是我摘录msdn上的
DWORD GetTickCount(VOID)Parameters
This function has no parameters. Return Values
The return value is the number of milliseconds that have elapsed since the system was started. Res
The following table describes the resolution of the system timer. System Resolution 
Windows NT 3.5 and later The system timer runs at approximately 10ms. 
Windows NT 3.1 The system timer runs at approximately 16ms. 
Windows 95 and later The system timer runs at approximately 55ms. 我想精确到4毫秒。好像不行是吗?

解决方案 »

  1.   

    GetTickCount有问题.确实是低是在15 ms.可以使用这个;
    class CTimeCounter
    {
    public:
        CTimeCounter(int nScaleSecond = 1000)
        {
    m_nScaleSecond = nScaleSecond;
    Begin();
        }
    void Begin()
    {
            QueryPerformanceFrequency(&m_nFreq);
            QueryPerformanceCounter(&m_nBeginTime);
    }
        __int64 End()
        {
    LARGE_INTEGER nEndTime;
    QueryPerformanceCounter(&nEndTime);
    return (__int64)(nEndTime.QuadPart - m_nBeginTime.QuadPart)*m_nScaleSecond/m_nFreq.QuadPart;
        }
    protected:
    int m_nScaleSecond;
        LARGE_INTEGER m_nFreq;
        LARGE_INTEGER m_nBeginTime;
    };
    CTimeCounter tc; // 或者CTimeCounter tm(1000000); 精确到1/1000000秒
    // 你的代码
    __int64 nCount = tc.End();
    tc.Begin(); //重新开始计数
    ....nCount = tc.End();
      

  2.   

    精度0.001微秒
    DWORD st,et;//起始时间,终止时间
    _asm{
    RDTSC
    mov st,eax
    }
    <..你的代码..>
    _asm{
    RDTSC
    mov et,eax
    }