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

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Matlab中,有几种方法可以计算程序运行时间。首先是使用cputime函数,它返回Matlab启动以来的CPU时间。你可以在代码执行前保存当前的CPU时间,然后在代码执行结束后用cputime减去之前保存的数值,就可以获取程序实际运行时间。例如: t0 = cputime; n = 100000; total = 0; for i = 1:n total = total + i; end elapsed_time = cputime - t0 这样,elapsed_time就是程序运行时间。\[1\] 另一种方法是使用tic和toc函数。tic函数用在程序代码之前,启动一个计时器;toc函数放在程序代码末尾,终止计时器并返回tic启动以来的总时间。例如: tic; n = 100000; total = 0; for i = 1:n total = total + i; end toc 这样,返回的Elapsed time就是程序运行时间。\[1\] 还有一种方法是使用etime函数,它可以计算两个日期向量之间的时间差。结合clock函数,可以用来确定程序代码的运行时间。例如: t = clock; n = 100000; total = 0; for i = 1:n total = total + i; end etime(clock, t) 这样,返回的ans就是程序运行时间。\[1\] 另外,你还可以使用clock和etime命令来计算程序运行时间。例如: t1 = clock; figure, surf(peaks(40)); t2 = clock; t = etime(t2, t1); disp(\['程序运行时间为:', num2str(t), '秒'\]); 这样,t就是程序运行时间。\[2\] 总结起来,你可以在程序的开始处使用tic函数,在程序的结束处使用toc函数来计算程序运行时间。另外,你还可以使用cputime函数或etime函数来计算程序运行时间。\[3\] #### 引用[.reference_title] - *1* [matlab 计算程序运行时间](https://blog.csdn.net/shenziheng1/article/details/51122790)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [MATLAB计算程序运行所需的时间](https://blog.csdn.net/yangSHU21/article/details/131343527)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Matlab中求程序运行时间三种方法](https://blog.csdn.net/My_Ling/article/details/118542055)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值