我用GetTickCount(),一个来回有时时间差为0。
似乎GetTickCount()针对的计时变量本身就有更新频率,导致10ms以内的时间差可能被忽略。
所以希望有更好的方法。

解决方案 »

  1.   

    高精度时控函数QueryPerformanceFrequency,QueryPerformanceCounter
    使用:LARGE_INTEGER m_nFreq;
              LARGE_INTEGER m_nBeginTime;
              LARGE_INTEGER nEndTime;
              QueryPerformanceFrequency(&m_nFreq); // 获取时钟周期
              QueryPerformanceCounter(&m_nBeginTime); // 获取时钟计数
              Sleep(100);
              QueryPerformanceCounter(&nEndTime);
         cout << (nEndTime.QuadPart-m_nBeginTime.QuadPart)*1000/m_nFreq.QuadPart << endl;
    原理:CPU上也有一个计数器,以机器的clock为单位,可以通过rdtsc读取,而不用中断,因此其精度与系统时间相当。
    精度:计算机获取硬件支持,精度比较高,可以通过它判断其他时间函数的精度范围。
      

  2.   

    timegettime,或者QueryPerformanceFrequency,QueryPerformanceCounter
      

  3.   

    Windows又不是实时操作系统  哪能那么精确
      

  4.   

    inline   unsigned   __int64   GetCycleCount()   
      {   
        __asm   _emit   0x0F   
        __asm   _emit   0x31   
      }   
        
      以后在需要计数器的场合,可以像使用普通的Win32   API一样,调用两次GetCycleCount函数,比较两个返回值的差,像这样:   
        
      unsigned   long   t;   
      t   =   (unsigned   long)GetCycleCount();   
      //Do   Something   time-intensive   ...   
      t   -=   (unsigned   long)GetCycleCount();   
      

  5.   

    在要测时的程序的前后调用一个函数写log日志,把时间以时间戳的形式输出,精确到毫秒就OK了啊!
      

  6.   

    class CStopWatch
    {
    public:
        CStopWatch()
        {
            QueryPerformanceFrequency(&perfFreq_);
            //TRACE1("CStopWatch::CStopWatch(): perfFreq_.QuadPart = %u.\n", perfFreq_.QuadPart);
            Start();
        }    void Start()
        {
            QueryPerformanceCounter(&perfStart_);
            //TRACE1("CStopWatch::Start(): perfStart_.QuadPart = %u.\n", perfStart_.QuadPart);
        }    __int64 Now() const
        {
            LARGE_INTEGER perfNow;
            QueryPerformanceCounter(&perfNow);
            //TRACE1("CStopWatch::Now(): perfNow.QuadPart = %u.\n", perfNow.QuadPart);        // @返回毫秒值
            return(((perfNow.QuadPart - perfStart_.QuadPart) * 1000) / perfFreq_.QuadPart);
        }    __int64 NowInMicro() const
        {
            LARGE_INTEGER perfNow;
            QueryPerformanceCounter(&perfNow);
            //TRACE1("CStopWatch::NowInMicro(): perfNow.QuadPart = %u.\n", perfNow.QuadPart);        // @返回毫微值
            return(((perfNow.QuadPart - perfStart_.QuadPart) * 1000000) / perfFreq_.QuadPart);
        }private:
        LARGE_INTEGER perfFreq_;
        LARGE_INTEGER perfStart_;
    };
    可用类,现成的
      

  7.   

    提取线程执行的有效时间,精确到100ns,摘自windows核心编程。
    // 定义全局函数,用以转换
    unsigned __int64 FileTimeToQuadWord(PFILETIME pft) {
    return (Int64ShllMod32(Int64ShllMod32(pft->dwHighDateTime, 31), 1) | pft->dwLowDateTime);
    }
    计时代码执行时间 FILETIME ftKernelTimeStart, ftKernelTimeEnd;
    FILETIME ftUserTimeStart, ftUserTimeEnd;
    FILETIME ftDummy;
    unsigned __int64 qwKernelTimeElapsed, qwUserTimeElapsed; // get starting times
    GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy,
    &ftKernelTimeStart, &ftUserTimeStart); // get ending times
    GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy,
    &ftKernelTimeEnd, &ftUserTimeEnd); // 需要计时的程序执行的代码放在此处 // 计时程序执行结束 // get the elapsed kernel and user times by converting the start
    // and end times from FILETIMEs to quad words, and then subtract
    // the start times from the end times.
    qwKernelTimeElapsed = FileTimeToQuadWord(&ftKernelTimeEnd) - 
    FileTimeToQuadWord(&ftKernelTimeStart); qwUserTimeElapsed = FileTimeToQuadWord(&ftUserTimeEnd) - 
    FileTimeToQuadWord(&ftUserTimeStart); // get total time duration by adding the kernel and user times.
    qwTotalTimeElapsed = qwKernelTimeElapsed + qwUserTimeElapsed;
      

  8.   

    inline unsigned __int64 GetCycleCount()
    {
    _asm _emit 0x0f
    _asm _emit 0x31
    }class KTimer
    {
    unsigned __int64 m_startcycle;

    public:
    unsigned __int64 m_overhead; KTimer()
    {
    m_overhead = 0;
    Start();
    m_overhead = Stop();
    } void Start()
    {
    m_startcycle = GetCycleCount();
    } unsigned __int64 Stop()
    {
    return GetCycleCount() - m_startcycle - m_overhead;
    }
    };void showCpuSpeed()
    {
    KTimer timer;
    timer.Start();
    Sleep(2000);
    unsigned __int64 cpuspeed10 = timer.Stop() / 200000;
    printf("CPU Speed : %d.%d MHz\n", cpuspeed10 / 10, cpuspeed10 % 10);
    }
    用这段函数试试
      

  9.   

    或者看看这篇文章
    http://blog.daviesliu.net/2004/07/29/041050/
      

  10.   

    RDTSC->_Start
    ...
    RDTSC->_End(_End-_Start)*1000/(2*2<<32)结果始终为0ms(此处假设CPU频率为2GHz);
    也就是说CPU频率越快,其计算结果为0的可能性越大,对吗?
      

  11.   

    直接给俺在2楼的回复无视了
    这里给段代码,摘自msdn// crt_clock.c
    /* This example prompts for how long
     * the program is to run and then continuously
     * displays the elapsed time for that period.
     */#include <stdio.h>
    #include <stdlib.h>
    #include <time.h>void sleep( clock_t wait );int main( void )
    {
       long    i = 6000000L;
       clock_t start, finish;
       double  duration;   /* Delay for a specified time. */
       printf( "Delay for three seconds\n" );
       sleep( (clock_t)3 * CLOCKS_PER_SEC );
       printf( "Done!\n" );   /* Measure the duration of an event. */
       printf( "Time to do %ld empty loops is ", i );
       start = clock();
       while( i-- ) 
          ;
       finish = clock();
       duration = (double)(finish - start) / CLOCKS_PER_SEC;
       printf( "%2.1f seconds\n", duration );
    }/* Pauses for a specified number of milliseconds. */
    void sleep( clock_t wait )
    {
       clock_t goal;
       goal = wait + clock();
       while( goal > clock() )
          ;
    }
      

  12.   

    Conry兄说的在MSDN上有,会给你加分的。^^
    不过计算结果还是0.
      

  13.   

    使用高精度的媒体时钟  //start 
      LARGE_INTEGER Start_Count;
      if(! QueryPerformanceCounter( &Start_Count ))
      {
       TRACE(0);
      }  这里加入测试代码/调用函数 如Sleep(1000);  //stop
      LARGE_INTEGER Stop_Count;
      if(! QueryPerformanceCounter( &Stop_Count ) )
      {
        TRACE(0);
      }  //consume 
      LARGE_INTEGER Consume_Count;
      Consume_Count.QuadPart = Stop_Count.QuadPart - Start_Count.QuadPart;  LARGE_INTEGER PerformanceFreq;
      if(!QueryPerformanceFrequency( &PerformanceFreq ) )
      {
        TRACE(0);
      }
      //consume times in ms
      double msTimes = (double)(Consume_Count.QuadPart) / (double)(PerformanceFreq.QuadPart);
      

  14.   

    何必这么麻烦?其实没你计算再精确,最后还是由操作系统调试执行的,所以要想更加精确可以使用下面方法更加实效!dwBegin = GetTickCount();
    for (i = 0; i < 1000; i++)
       CallFunc(...);
    dwEnd = GetTickCount();printf("CallFunc time: %d/1000 ms\n", dwEnd - dwBegin);