c++计时器

//Windows系统下time(),clock(),timeGetTime(),GetTickCount(),QueryPerformanceCounter()来计时
#include <stdio.h>  
#include <windows.h>  
#include <time.h>                   //time_t time()  clock_t clock()  
#include <Mmsystem.h>               //timeGetTime()  
#pragma comment(lib, "Winmm.lib")   //timeGetTime()  
   
//使用方法:将Sleep()函数换成需要测试运行时间的函数即可。
int main()  
{  
    //用time()来计时  秒  
    time_t timeBegin, timeEnd;  
    timeBegin = time(NULL);  
    Sleep(1000);  
    timeEnd = time(NULL);  
    printf("%dn", timeEnd - timeBegin);  
       
       
    //用clock()来计时  毫秒  
    clock_t  clockBegin, clockEnd;  
    clockBegin = clock();  
    Sleep(800);  
    clockEnd = clock();  
    printf("%dn", clockEnd - clockBegin);  
       
       
    //用timeGetTime()来计时  毫秒  
    DWORD  dwBegin, dwEnd;  
    dwBegin = timeGetTime();  
    Sleep(800);  
    dwEnd = timeGetTime();  
    printf("%dn", dwEnd - dwBegin);  
       
       
    //用GetTickCount()来计时  毫秒  
    DWORD  dwGTCBegin, dwGTCEnd;  
    dwGTCBegin = GetTickCount();  
    Sleep(800);  
    dwGTCEnd = GetTickCount();  
    printf("%dn", dwGTCEnd - dwGTCBegin);  
       
    
    //用QueryPerformanceCounter()来计时  微秒  
    LARGE_INTEGER  large_interger;  
    double dff;  
    __int64  c1, c2;  
    QueryPerformanceFrequency(&large_interger);  
    dff = large_interger.QuadPart;  
    QueryPerformanceCounter(&large_interger);  
    c1 = large_interger.QuadPart;  
    Sleep(800);  
    QueryPerformanceCounter(&large_interger);  
    c2 = large_interger.QuadPart;  
    printf("本机高精度计时器频率%lfn", dff);  
    printf("第一次计时器值%I64d 第二次计时器值%I64d 计时器差%I64dn", c1, c2, c2 - c1);  
    printf("计时%lf毫秒n", (c2 - c1) * 1000 / dff);  
 
     
    return 0;  
}  


C++中,可以使用Windows API中的SetTimer函数来创建一个计时器,该函数的原型如下: ``` UINT_PTR SetTimer( HWND hWnd, // 窗口句柄 UINT_PTR nIDEvent, // 计时器标识符 UINT uElapse, // 计时间隔,单位为毫秒 TIMERPROC lpTimerFunc// 计时器回调函数 ); ``` 其中,hWnd参数是要接收计时器消息的窗口句柄,nIDEvent是计时器的标识符,可以用于取消计时器,uElapse是计时间隔,单位是毫秒,lpTimerFunc是计时器回调函数,当计时器到达指定时间时,系统会发送一个WM_TIMER消息给指定窗口,同时调用回调函数。 以下是一个简单的示例代码,实现了每秒钟在窗口标题中显示当前时间: ```c++ #include <Windows.h> #include <string> LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CREATE: // 创建一个每秒钟触发一次的计时器 SetTimer(hWnd, 1, 1000, NULL); break; case WM_TIMER: if (wParam == 1) { // 获取当前时间 SYSTEMTIME st; GetLocalTime(&st); std::wstring title = L"Current Time: "; title += std::to_wstring(st.wHour) + L":" + std::to_wstring(st.wMinute) + L":" + std::to_wstring(st.wSecond); // 更新窗口标题 SetWindowText(hWnd, title.c_str()); } break; case WM_DESTROY: // 销毁计时器 KillTimer(hWnd, 1); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // 注册窗口类 WNDCLASS wc = { 0 }; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszClassName = L"TimerDemo"; RegisterClass(&wc); // 创建窗口 HWND hWnd = CreateWindow(L"TimerDemo", L"Timer Demo", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } // 显示窗口 ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // 消息循环 MSG msg = { 0 }; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } ``` 该示例代码中,我们在窗口创建时创建了一个每秒钟触发一次的计时器,并在计时器回调函数中获取当前时间,并将其显示在窗口标题中。在窗口销毁时,我们销毁了计时器
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值