gpt4 book ai didi

自应用程序启动以来获取时间的 c++ 可移植方法?

转载 作者:行者123 更新时间:2023-11-28 01:07:25 25 4
gpt4 key购买 nike

在 C++ 中,有什么好的可移植方式(可能使用某些库)来获取自应用程序启动以来的时间,并且精度至少为亚毫秒级?

谢谢!

编辑:由于亚毫秒级在所有平台上都不可能实现,那么如何才能尽可能准确地达到亚毫秒级呢?

最佳答案

Boost Date Time可以提供帮助,但正如 Damon 已经在评论中指出的那样,这是一个相当依赖于平台的问题,并且您的最低要求比使用最广泛的操作系统可以支持的要求更苛刻。

因此,由 Boost 提供的抽象很有帮助。当然还有无数其他图书馆,但是Boost Date Time库有纳秒分辨率选项(如果可用),但您需要在构建时启用它:

Compilation Options

By default the posix_time system uses a single 64 bit integer internally to provide a microsecond level resolution. As an alternative, a combination of a 64 bit integer and a 32 bit integer (96 bit resolution) can be used to provide nano-second level resolutions. The default implementation may provide better performance and more compact memory usage for many applications that do not require nano-second resolutions.

To use the alternate resolution (96 bit nanosecond) the variable BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG must be defined in the library users project files (ie Makefile, Jamfile, etc). This macro is not used by the Gregorian system and therefore has no effect when building the library.

关于自应用程序启动以来获取时间的 c++ 可移植方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5444369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com