C++每日一問:如何獲取時(shí)間?精度如何?

字號(hào):

Q:如何獲取時(shí)間?精度如何?
    A:1 使用time_t time( time_t * timer ) 精確到秒
     2 使用clock_t clock() 得到的是CPU時(shí)間 精確到1/CLOCKS_PER_SEC秒
     3 計(jì)算時(shí)間差使用double difftime( time_t timer1, time_t timer0 )
     4 使用DWORD GetTickCount() 精確到毫秒
     5 如果使用MFC的CTime類,可以用CTime::GetCurrentTime() 精確到秒
     6 要獲取高精度時(shí)間,可以使用
     BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)
     獲取系統(tǒng)的計(jì)數(shù)器的頻率
     BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
     獲取計(jì)數(shù)器的值
     然后用兩次計(jì)數(shù)器的差除以Frequency就得到時(shí)間。
     7 還有David的文章中提到的方法:
     Multimedia Timer Functions
     The following functions are used with multimedia timers.
     timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime