我想在C++中计算一段代码执行的时间
请问如何实现?
用什么函数?

解决方案 »

  1.   

    GetTickCount(),看这段代码开始与结束的差值!
    If you need a higher resolution timer, use amultimedia timer or a high-resolution timer. 
      

  2.   

    来迟了,
    GetTickCountGetTickCount
    The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. It is limited to the resolution of the system timer. If you need a higher resolution timer, use amultimedia timer or a high-resolution timer. 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. 
      

  3.   

    我来的更晚。
    开始计时:DWORD dwStart = GetTickCount();
    结束:GetTickCount() - dwStart;
      

  4.   

    // To calculate elapsed time 
    CTime TimeStart,TimeEnd,TimeSecond,TimeMinute;
    CTimeSpan SpanElapsed;
             TimeStart = CTime::GetCurrentTime();
             // ... perform time-consuming task TimeEnd = CTime::GetCurrentTime();
             SpanElapsed = TimeEnd - TimeStart;
    TimeSecond=SpanElapsed.GetTotalSeconds();
    TimeMinute=SpanElapsed.GetTotalMinutes();
      

  5.   

    For higher time resolution, the following methods may help youQueryPerformanceFrequency() & QueryPerformanceCounter()you can retrieve system frequencies, starting point frequency count and current frequency count. Use current frequency count minus starting point frequency count, then divide by system frequency, you can get the accurate time cost.