栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++利用windows函数实现计时示例

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++利用windows函数实现计时示例

复制代码 代码如下:
//Windows系统下可以用 time(),clock(),timeGetTime(),GetTickCount(),QueryPerformanceCounter()来对一段程序代码进行计时

#include
#include
#include                    //time_t time()  clock_t clock()   
#include                //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("第一次计时器值%I64dn第二次计时器值%I64dn计时器差%I64dn", c1, c2, c2 - c1);
    printf("计时%lf毫秒nn", (c2 - c1) * 1000 / dff);
    return 0;
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/65736.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号