雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

c++利用windows函數(shù)實現(xiàn)計時示例

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊:

復(fù)制代碼 代碼如下:

//Windows系統(tǒng)下可以用 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()函數(shù)換成需要測試運行時間的函數(shù)即可。

int main()
{   //用time()來計時,以秒為單位
    time_t timeBegin, timeEnd;
    timeBegin = time(NULL);
    Sleep(1000);
    timeEnd = time(NULL);
    printf("%d\n", timeEnd - timeBegin);


    //用clock()來計時,以毫秒為單位
    clock_t  clockBegin, clockEnd;
    clockBegin = clock();
    Sleep(800);
    clockEnd = clock();
    printf("%d\n", clockEnd - clockBegin);


    //用timeGetTime()來計時,以毫秒為單位
    DWORD  dwBegin, dwEnd;
    dwBegin = timeGetTime();
    Sleep(800);
    dwEnd = timeGetTime();
    printf("%d\n", dwEnd - dwBegin);


    //用GetTickCount()來計時,以毫秒為單位
    DWORD  dwGTCBegin, dwGTCEnd;
    dwGTCBegin = GetTickCount();
    Sleep(800);
    dwGTCEnd = GetTickCount();
    printf("%d\n", 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("本機高精度計時器頻率%lf\n", dff);
    printf("第一次計時器值%I64d\n第二次計時器值%I64d\n計時器差%I64d\n", c1, c2, c2 - c1);
    printf("計時%lf毫秒\n\n", (c2 - c1) * 1000 / dff);
    return 0;
}



上一篇:c++作用域運算符用法(全局變量和局部變量)

欄    目:C語言

下一篇:c語言標(biāo)準(zhǔn)庫中字符轉(zhuǎn)換函數(shù)和數(shù)字轉(zhuǎn)換函數(shù)

本文標(biāo)題:c++利用windows函數(shù)實現(xiàn)計時示例

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3672.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有