在C++ 中如何计算一段代码执行的时间?

蟑螂工作室 2002-12-03 10:32:20
我想在C++中计算一段代码执行的时间
请问如何实现?
用什么函数?
...全文
194 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rigel 2002-12-04
  • 打赏
  • 举报
回复
我来的更晚。
开始计时:DWORD dwStart = GetTickCount();
结束:GetTickCount() - dwStart;
JiaoKoala 2002-12-04
  • 打赏
  • 举报
回复
For higher time resolution, the following methods may help you

QueryPerformanceFrequency() & QueryPerformanceCounter()

you can retrieve system frequencies, starting point frequency count and current frequency count. Use current frequency count minus starting point frequency count, then divide by system frequency, you can get the accurate time cost.
yjiwxg 2002-12-04
  • 打赏
  • 举报
回复
// To calculate elapsed time
CTime TimeStart,TimeEnd,TimeSecond,TimeMinute;
CTimeSpan SpanElapsed;
TimeStart = CTime::GetCurrentTime();
// ... perform time-consuming task

TimeEnd = CTime::GetCurrentTime();
SpanElapsed = TimeEnd - TimeStart;
TimeSecond=SpanElapsed.GetTotalSeconds();
TimeMinute=SpanElapsed.GetTotalMinutes();
binbin 2002-12-03
  • 打赏
  • 举报
回复
来迟了,
GetTickCount

GetTickCount
The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. It is limited to the resolution of the system timer. If you need a higher resolution timer, use amultimedia timer or a high-resolution timer.

DWORD GetTickCount(VOID)

Parameters
This function has no parameters.

Return Values
The return value is the number of milliseconds that have elapsed since the system was started.
wwwqqq 2002-12-03
  • 打赏
  • 举报
回复
GetTickCount(),看这段代码开始与结束的差值!
If you need a higher resolution timer, use amultimedia timer or a high-resolution timer.

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧