天天看点

VS程序中计算程序运行时间

原文地址:https://blog.csdn.net/weixin_30496431/article/details/95700974

有时候在设计程序完了之后需要计算程序运行的时间。

  这时候可以使用Windows的库函数 GetIickCount(),其头文件为<windows.h>  

#include<iostream>
#include<windows.h>
int main()
{
DWORD start_time=GetTickCount();
{
//此处为被测试代码
}
DWORD end_time=GetTickCount();
cout<<"The run time is:"<<(end_time-start_time)*1.00/1000<<"s!"<<endl;//输出运行时间
return 0;
}      

因为 CPU 周期就是毫秒为单位的

精确到毫秒的方法可以用 API 函数 GetTickCount()

Timer 控件就算你设置成 1ms 激发一次但是实际也是 33ms 激发一次

要想实现真正的毫秒级精确度需要用 GetTickCount()

比如

t1 = GetTickCount

...……// 中间代码

t2 = GetTickCount

t3 = t2 - t1

这里t3就是中间代码的执行时间,单位为毫秒

GetTickCount()

获取系统执行时间,返回毫秒值

超过39.7天清零

————————————————

版权声明:本文为CSDN博主「foreverhuylee」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/foreverhuylee/article/details/22323213