C/C++ 计算时间差的五种方法 / Qt 计算时间差的两种方法

来源:互联网 发布:三月软件工作室面试 编辑:程序博客网 时间:2024/05/16 11:32

转自:http://blog.csdn.net/chy555chy/article/details/53405072

Qt计算时间的两种方法:

  • QTime elapsed() : ms
  • QTime currentTime() : ms

C++计算时间的五种方法:

  • clock() : ms
  • GetTickCount() : ms
  • gettimeofday(time_val*, NULL) : us
  • QueryPerformanceFrequency(LARGE_INTEGER*) & QueryPerformanceCounter(LARGE_INTEGER*) : us
  • time(NULL) : s
#include <QThread>#include <QTime>#include <sys/time.h>   //clock, gettimeofday, time#include <windows.h>    //Sleep, GetTickCount, timeGetTime, QueryPerformanceCounter#include <QDebug>//#pragma comment(lib, "winmm.lib") //timeGetTime, 但是这个库Windows或Qt或VS2015都没有自带const unsigned long SLEEP_TIME_MILL = 1000;/** * QTime的精度为ms级 */void calcByQtimeElapsed() {    QTime time;    time.start();    QThread::msleep(SLEEP_TIME_MILL);    int timeElapsed = time.elapsed();    qDebug()<<"QTime.start & QTime.elspsed ="<<timeElapsed<<"ms";}/** * QTime的精度为ms级 */void calcByQtimeCurrentTime() {    QTime startTime = QTime::currentTime();    QThread::msleep(SLEEP_TIME_MILL);    QTime stopTime = QTime::currentTime();    int elapsed = startTime.msecsTo(stopTime);    qDebug()<<"QTime.currentTime ="<<elapsed<<"ms";}/** * clock的精度为ms级 */void calcByClock() {    clock_t startTime = clock();    Sleep(SLEEP_TIME_MILL);    clock_t endTime = clock();    clock_t elapsed = endTime - startTime;    qDebug()<<"clock ="<<elapsed<<"ms";}/** * GetTickCount的精度为ms级 */void calcByTickCount() {    //返回自开机以来经历的毫秒数    DWORD startTime = GetTickCount();    Sleep(SLEEP_TIME_MILL);    DWORD stopTime = GetTickCount();    DWORD elapsed = stopTime - startTime;    qDebug()<<"GetTickCount = "<<elapsed<<"ms";}///**// * timeGetTime的精度为ms级// * 必须添加Winmm.lib, 否则编译报错undefined reference// *///void calcByTimeGetTime() {//    //返回自开机以来经历的毫秒数//    DWORD startTime = timeGetTime();//    Sleep(SLEEP_TIME_MILL);//    DWORD stopTime = timeGetTime();//    DWORD elapsed = stopTime - startTime;//    qDebug()<<"timeGetTime = "<<elapsed<<"ms";//}/** * gettimeofday的精度为us级 */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 ="<<elapsed<<"us";}/** * QueryPerformanceCounter的精度为us级 */void calcByQueryPerformanceCounter() {    LARGE_INTEGER frequency, startCount, stopCount;    WINBOOL ret;    //返回性能计数器每秒滴答的个数    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 ="<<elapsed<<"us";    }}/** * time(NULL)精确度为s级 */void calcByTime() {    time_t startTime = time(NULL);    Sleep(SLEEP_TIME_MILL);    time_t stopTime = time(NULL);    long elapsed = stopTime - startTime;    qDebug()<<"time(NULL) ="<<elapsed<<"s";}int main(int argc, char *argv[]){    calcByQtimeElapsed();    calcByQtimeCurrentTime();    calcByClock();    calcByTickCount();//    calcByTimeGetTime();    calcByGetTimeOfDay();    calcByQueryPerformanceCounter();    calcByTime();    system("pause");    return 0;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125

这里写图片描述


阅读全文
0 0