记录程序运行时间 精确到ms

zedzhao 2008-12-24 10:57:03
好像用clock()人家说是精确到s
GetTickCount() 我试了 不行的 不知道该包含哪个头文件 或者有什么别的方法记录时间
越精确越好 也不用特别精确 一个课程设计用的
...全文
301 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zedzhao 2008-12-24
  • 打赏
  • 举报
回复
GetTickCount()在MSDN里我有找到,不过clock()没找到,
我测试了下 好像2个精度一样的
SKYNORAIN 2008-12-24
  • 打赏
  • 举报
回复
多查查MSDN
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. To obtain the system timer resolution, use the GetSystemTimeAdjustment function.


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.

Remarks
The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days.

If you need a higher resolution timer, use a multimedia timer or a high-resolution timer.

To obtain the time elapsed since the computer was started, retrieve the System Up Time counter in the performance data in the registry key HKEY_PERFORMANCE_DATA. The value returned is an 8-byte value. For more information, see Performance Monitoring.

Example Code
The following example demonstrates how to use a this function to wait for a time interval to pass. Due to the nature of unsigned arithmetic, this code works correctly if the return value wraps one time. If the difference between the two calls to GetTickCount is more than 49.7 days, the return value could wrap more than one time and this code will not work; use the system time instead. Note that TIMELIMIT is defined as the time interval of interest to the application, in milliseconds.

DWORD dwStart = GetTickCount();

// Stop if this has taken too long
if( GetTickCount() - dwStart >= TIMELIMIT )
Cancel();

Requirements
Client Requires Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
Server Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server.
Header Declared in Winbase.h; include Windows.h.

Library Link to Kernel32.lib.

DLL Requires Kernel32.dll.
zedzhao 2008-12-24
  • 打赏
  • 举报
回复
汗~~~~没有
我以为c++下的都不用.h的了..
#include <cwindows> 不行就不知道怎么办了...

可是 GetTickCount() 和 clock()的精度一样吗?
agaric 2008-12-24
  • 打赏
  • 举报
回复
... 有没有包含windows.h??
zedzhao 2008-12-24
  • 打赏
  • 举报
回复
给个例子行吗
主要就是 好像这些API用不了 提示
error C3861: “GetTickCount”: 找不到标识符
funnybunny 2008-12-24
  • 打赏
  • 举报
回复
调用2次API的GetSystemTime或者 GetLocalTime可以吧,详见MSDN~

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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