gpt4 book ai didi

c++ - 使用 C++ 以毫秒为单位查找实时时间的好方法是什么?

转载 作者:IT王子 更新时间:2023-10-29 01:06:35 26 4
gpt4 key购买 nike

我目前正在使用 time.h 库提供的 clock() 函数。它为我提供了毫秒级的时间精度。但是,它的时序是基于 CPU 时钟周期的。我需要一个函数,而不是将 CPU 周期用作 clock(),而是使用精确到毫秒的系统实时时间。

我正在使用带有 gcc 编译器的 linux。

最佳答案

#include <sys/time.h>

...

timeval tv;
gettimeofday (&tv, NULL);
return double (tv.tv_sec) + 0.000001 * tv.tv_usec;

关于c++ - 使用 C++ 以毫秒为单位查找实时时间的好方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4370776/

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