gpt4 book ai didi

c++ - 如何在 C++ 中以微秒为单位获取系统时钟?

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:38 30 4
gpt4 key购买 nike

我正在以控制台模式使用 Visual C++/CLR 开发一个项目。如何获得以微秒为单位的系统时钟?我想显示 hours:minutes:seconds:microseconds

以下程序运行良好,但与其他平台不兼容:

#include <stdio.h>
#include <sys/time.h>
int main()
{
struct timeval tv;
struct timezone tz;
struct tm *tm;
gettimeofday(&tv, &tz);
tm=localtime(&tv.tv_sec);
printf(" %d:%02d:%02d %ld \n", tm->tm_hour, tm->tm_min,tm->tm_sec, tv.tv_usec);
return 0;
}

最佳答案

您可以使用来自 Boost 的 ptime microsec_clock::local_time()

文档可用here .

之后可以使用std::string to_iso_extended_string(ptime)将返回的时间显示为字符串,也可以直接使用ptime的成员进行格式化自己输出。

无论如何,值得注意的是:

Win32 systems often do not achieve microsecond resolution via this API. If higher resolution is critical to your application test your platform to see the achieved resolution.

所以我想这取决于您要求“时钟”的精确度。

关于c++ - 如何在 C++ 中以微秒为单位获取系统时钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10382902/

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