C++获取程序执行时间的五个方法对比

五种获取C/C++程序执行时间的方法对比如下:

核心函数                                      头文件        函数库      精度     准确度
QueryPerformanceCounter        windows.h       API         us       非常准确
GetTickTount                              windows.h       API         ms       准确
clock                                           time.h           C函数        ms       较准确
time                                            time.h          C函数          s         很准确
ftime                                         sys/timeb.h     C函数        ms       较准确

在windows平台下建议使用GetTickCount,当对精度和准确度要求高可以用QueryPerformanceCounter,Linux平台下建议使用ftime,
本程序均在windows平台下运行,五种获取程序执行时间的代码如下:

#include <iostream>  
#include <fstream>  
#include <ctime>  
#include <windows.h>  
#include <tchar.h>  
#include <sys/timeb.h>  
#include <time.h>  
#include <cmath>  
#include <stdio.h>  
  
using namespace std;  

/**************************获取系统时间*************************/  
void MyGetSystemTime()  
{  
    SYSTEMTIME currentTime;  
    GetSystemTime(¤tTime);  
    printf("time: %u/%u/%u %u:%u:%u:%u %d\n",   
        currentTime.wYear,currentTime.wMonth,currentTime.wDay,   
        currentTime.wHour,currentTime.wMinute,currentTime.wSecond,   
        currentTime.wMilliseconds,currentTime.wDayOfWeek);  
}  

  
/****************自己实现的定时器,要比Sleep准确****************/  
void MySleep(DWORD dwMilliseconds)  
{  
    LARGE_INTEGER litmp;  
    LONGLONG QPart1;  
    QueryPerformanceCounter(&litmp);  
    QPart1 = litmp.QuadPart;//获得初始值  
  
    LONGLONG QPart2;  
    double dfMinus, dfFreq, dfTim;  
    QueryPerformanceFrequency(&litmp);  
    dfFreq = (double)litmp.QuadPart;//获得计数器的时钟频率  
  
    do  
    {  
        QueryPerformanceCounter(&litmp);  
        QPart2 = litmp.QuadPart;//获得中止值  
        dfMinus = (double)(QPart2-QPart1);  
        dfTim = dfMinus / dfFreq * 1000;//获得对应的时间值,单位为毫秒  
    }while(dfTim < dwMilliseconds);  
}  
  
int main()  
{  

/**************QueryPerformanceCounter(),windows.h,API,us****************///精度最高  
     LARGE_INTEGER nFreq;  
     LARGE_INTEGER nBeginTime;  
     LARGE_INTEGER nEndTime;  
  
     double time;  
     QueryPerformanceFrequency(&nFreq);  
     QueryPerformanceCounter(&nBeginTime);   
     Sleep(1000);  
     QueryPerformanceCounter(&nEndTime);  
  
     time=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;  
     printf("%f\n",time);  
  

/**************GetTickCount(),windows.h,API,ms****************///首选  
    DWORD start, end;  
    start = GetTickCount();  
    MySleep(1000);  
    end = GetTickCount();  
    printf("time: %d ms\n", end - start);  
  

/**************clock(),time.h,C函数,ms,不太准****************/  
    clock_t start, end;  
    start = clock();  
    Sleep(1000);  
    end = clock();  
    printf("time: %d ms\n", end - start);  
  

/**************time(),time.h,C函数,s****************/  
    time_t start, end;  
    start = time(NULL);  
    Sleep(1000);  
    end = time(NULL);  
    printf("time: %d ms\n", end - start);  
  

/**************ftime(),sys/timeb.h,C函数,ms,不太准****************/  
    struct timeb startTime , endTime;  
    ftime(&startTime);  
    Sleep(1000);  
    ftime(&endTime);  
    printf("time: %d ms\n", (endTime.time-startTime.time)*1000 + (endTime.millitm - startTime.millitm));  
    return 0;  
}

文章转载自龙马谷论坛:C++获取程序执行时间的五个方法对比 - C/C++技术分享龙马谷

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值