- 注册时间
 - 2021-4-16
 
- 最后登录
 - 2023-8-11
 
- 在线时间
 - 3 小时
 
 
 
 
 
编程入门 
  
	- 龙马币
 - 36 
 
 
 
 
 | 
 
 
五种获取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;  
 
 - }
 
  复制代码 
 |   
 
 
 
 |