c++语言window函数如何使用方法,c++利用windows函数实现计时范例

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

计时怎样利用代码实现呢?以下是为大家分享的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;

}

【c++利用windows函数实现计时范例】相关文章:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值