C / C ++時差を計算する5つの方法/ Qt時差を計算する2つの方法



C C Five Ways Calculate Time Difference Qt Two Ways Calculate Time Difference



// - - - - - - - - [からの転送: 司馬懿中田 】—————————————– //
Qtが時間を計算するための2つの方法:

  1. QTime経過():ms
  2. QTime currentTime():ms

C ++で時間を計算する5つの方法:



  1. clock():ms
  2. GetTickCount():ミリ秒
  3. gettimeofday(time_val *、NULL):us
  4. QueryPerformanceFrequency(LARGE_INTEGER *)&QueryPerformanceCounter(LARGE_INTEGER *):us
  5. 時間(NULL):s
#include #include #include //clock, gettimeofday, time #include //Sleep, GetTickCount, timeGetTime, QueryPerformanceCounter #include //#pragma comment(lib, 'winmm.lib') //timeGetTime, but this library does not come with Windows, Qt or VS2015 const unsigned long SLEEP_TIME_MILL = 1000 /** * The accuracy of QTime is ms level */ void calcByQtimeElapsed() { QTime time time.start() QThread::msleep(SLEEP_TIME_MILL) int timeElapsed = time.elapsed() qDebug()<<'QTime.start & QTime.elspsed ='<'ms' } /** * The accuracy of QTime is ms level */ void calcByQtimeCurrentTime() { QTime startTime = QTime::currentTime() QThread::msleep(SLEEP_TIME_MILL) QTime stopTime = QTime::currentTime() int elapsed = startTime.msecsTo(stopTime) qDebug()<<'QTime.currentTime ='<'ms' } /** * The accuracy of clock is ms level */ void calcByClock() { clock_t startTime = clock() Sleep(SLEEP_TIME_MILL) clock_t endTime = clock() clock_t elapsed = endTime - startTime qDebug()<<'clock ='<'ms' } /** * The accuracy of GetTickCount is ms level */ void calcByTickCount() { //Returns the number of milliseconds since booting DWORD startTime = GetTickCount() Sleep(SLEEP_TIME_MILL) DWORD stopTime = GetTickCount() DWORD elapsed = stopTime - startTime qDebug()<<'GetTickCount = '<'ms' } ///** // * The accuracy of timeGetTime is ms level // * Winmm.lib must be added, otherwise the compilation error undefined reference // */ //void calcByTimeGetTime() { // //Returns the number of milliseconds since booting // DWORD startTime = timeGetTime() // Sleep(SLEEP_TIME_MILL) // DWORD stopTime = timeGetTime() // DWORD elapsed = stopTime - startTime // qDebug()<<'timeGetTime = '< //} /** * The accuracy of gettimeofday is us level */ void calcByGetTimeOfDay() { struct timeval startTime, stopTime gettimeofday(&startTime, NULL) Sleep(SLEEP_TIME_MILL) gettimeofday(&stopTime, NULL) long elapsed = (stopTime.tv_sec-startTime.tv_sec) * 1000000 + (stopTime.tv_usec - startTime.tv_usec) qDebug()<<'gettimeofday ='<'us' } /** * The accuracy of QueryPerformanceCounter is us level */ void calcByQueryPerformanceCounter() { LARGE_INTEGER frequency, startCount, stopCount WINBOOL ret //Returns the number of ticks per second of the performance counter ret = QueryPerformanceFrequency(&frequency) if(ret) { ret = QueryPerformanceCounter(&startCount) } Sleep(SLEEP_TIME_MILL) if(ret) { QueryPerformanceCounter(&stopCount) } if(ret) { LONGLONG elapsed = (stopCount.QuadPart - startCount.QuadPart) * 1000000 / frequency.QuadPart qDebug()<<'QueryPerformanceFrequency & QueryPerformanceCounter ='<'us' } } /** * time (NULL) accuracy is s level */ void calcByTime() { time_t startTime = time(NULL) Sleep(SLEEP_TIME_MILL) time_t stopTime = time(NULL) long elapsed = stopTime - startTime qDebug()<<'time(NULL) ='<'s' } int main(int argc, char *argv[]) { calcByQtimeElapsed() calcByQtimeCurrentTime() calcByClock() calcByTickCount() // calcByTimeGetTime() calcByGetTimeOfDay() calcByQueryPerformanceCounter() calcByTime() system('pause') return 0 }

画像