当前位置:编程学习 > C/C++ >>

C_C++ 各种计时函数

 对Windows平台下常用的计时函数进行总结,包括精度为秒、毫秒、微秒三种精度的5 种方法。分为在标准C/C++下的二种time()及clock(),标准C/C++所以使用的time()及clock()不仅可以用在Windows 系统,也可以用于Linux系统。

在Windows系统下三种,使用Windows提供的API接口timeGetTime()、 GetTickCount()及QueryPerformanceCounter()来完成。

 


[cpp]
#include <windows.h>    
#include <time.h> //time_t time()  clock_t clock()    
#include <Mmsystem.h>             //timeGetTime()    
#pragma comment(lib, "Winmm.lib")   //timeGetTime()    
 
int main(int argc, char* argv[]) 

    //用time()来计时  秒    
    time_t timeBegin, timeEnd;   
    timeBegin = time(NULL);   
    Sleep(800);   
    timeEnd = time(NULL);   
    printf("time %d\n", timeEnd - timeBegin);   
     
     
    //用clock()来计时  毫秒    
    clock_t  clockBegin, clockEnd;   
    clockBegin = clock();   
    Sleep(800);   
    clockEnd = clock();   
    printf("clock %d\n", clockEnd - clockBegin);   
     
     
    //用timeGetTime()来计时  毫秒    
    DWORD  dwBegin, dwEnd;   
    dwBegin = timeGetTime();   
    Sleep(800);   
    dwEnd = timeGetTime();   
    printf("timeGetTime %d\n", dwEnd - dwBegin);   
     
     
    //用GetTickCount()来计时  毫秒    
    DWORD  dwGTCBegin, dwGTCEnd;   
    dwGTCBegin = GetTickCount();   
    Sleep(800);   
    dwGTCEnd = GetTickCount();   
    printf("GetTickCount %d\n", dwGTCEnd - dwGTCBegin);   
     
     
    //用QueryPerformanceCounter()来计时  微秒    
    LARGE_INTEGER  large_interger;   
    double dff;   
    __int64  c1, c2;   
    //获取是否支持精确定时器。 定时器的频率  
    if( QueryPerformanceFrequency(&large_interger) ) 
    { 
        dff = large_interger.QuadPart;  
        //获取定时器的值  
        QueryPerformanceCounter(&large_interger);   
        c1 = large_interger.QuadPart;   
        Sleep(800);   
        //获取定时器的值  
        QueryPerformanceCounter(&large_interger);   
        c2 = large_interger.QuadPart;   
        printf("本机高精度计时器频率%lf\n", dff);   
        printf("第一次计时器值%I64d 第二次计时器值%I64d 计时器差%I64d\n", c1, c2, c2 - c1);   
        printf("计时%lf毫秒\n", (c2 - c1) * 1000 / dff);   
        printf("\n");    
    } 
 
    return 0;   

#include <windows.h> 
#include <time.h> //time_t time()  clock_t clock() 
#include <Mmsystem.h>             //timeGetTime() 
#pragma comment(lib, "Winmm.lib")   //timeGetTime() 

int main(int argc, char* argv[])
{
    //用time()来计时  秒 
    time_t timeBegin, timeEnd; 
    timeBegin = time(NULL); 
    Sleep(800); 
    timeEnd = time(NULL); 
    printf("time %d\n", timeEnd - timeBegin); 
   
   
    //用clock()来计时  毫秒 
    clock_t  clockBegin, clockEnd; 
    clockBegin = clock(); 
    Sleep(800); 
    clockEnd = clock(); 
    printf("clock %d\n", clockEnd - clockBegin); 
   
   
    //用timeGetTime()来计时  毫秒 
    DWORD  dwBegin, dwEnd; 
    dwBegin = timeGetTime(); 
    Sleep(800); 
    dwEnd = timeGetTime(); 
    printf("timeGetTime %d\n", dwEnd - dwBegin); 
   
   
    //用GetTickCount()来计时  毫秒 
    DWORD  dwGTCBegin, dwGTCEnd; 
    dwGTCBegin = GetTickCount(); 
    Sleep(800); 
    dwGTCEnd = GetTickCount(); 
    printf("GetTickCount %d\n", dwGTCEnd - dwGTCBegin); 
   
   
    //用QueryPerformanceCounter()来计时  微秒 
    LARGE_INTEGER  large_interger; 
    double dff; 
    __int64  c1, c2; 
    //获取是否支持精确定时器。 定时器的频率
    if( QueryPerformanceFrequency(&large_interger) )
    {
        dff = large_interger.QuadPart;
        //获取定时器的值
        QueryPerformanceCounter(&large_interger); 
        c1 = large_interger.QuadPart; 
        Sleep(800); 
        //获取定时器的值
        QueryPerformanceCounter(&large_interger); 
        c2 = large_interger.QuadPart; 
        printf("本机高精度计时器频率%lf\n", dff); 
      

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,