Windows 编译器上的 timespec
在 posix 上,可以使用 timespec 来计算准确的时间长度(如秒和毫秒)。不幸的是,我需要使用 Visual Studio 编译器迁移到 Windows。 VS time.h 库没有声明 timespec,所以我正在寻找其他选项。就搜索而言,是否可以使用时钟和 time_t,尽管我无法检查使用时钟计数来计算毫秒的精确度。
您如何/使用什么来计算操作中的时间消耗(如果可能,使用标准 C++ 库)?
On posix it is possible to use timespec to calculate accurate time length (like seconds and milliseconds). Unfortunately I need to migrate to windows with Visual Studio compiler. The VS time.h library doesn't declare timespec so I'm looking for other options. As far as could search is it possible to use clock and time_t although I could't check how precise is counting millisecons with clock counting.
What do you do/use for calculating time elapse in a operation (if possible using standards c++ library) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常使用函数 GetTickCount为此。
还有一个类似的线程: C++ 计时,自上一整秒以来的毫秒数
The function GetTickCount is usually used for that.
Also a similiar thread: C++ timing, milliseconds since last whole second
取决于你想要什么样的精度,我的理解是clock和time_t不精确到毫秒级别。类似地,GetTickCount() 也很常用(MS 文档说精确到 10-15 毫秒),但对于许多用途来说不够精确。
我使用 QueryPerformanceFrequency 和 QueryPerformanceCounter 用于准确的性能计时测量。
Depends on what sort of accuracy you want, my understanding is that clock and time_t are not accurate to the millisecond level. Similarly GetTickCount() is commonly used (MS docs say accurate to 10-15ms) but not sufficiently accurate for many purposes.
I use QueryPerformanceFrequency and QueryPerformanceCounter for accurate timing measurements for performance.