英文:
Epoch time in microseconds
问题
可以获取微秒级别的纪元时间吗?
我无法找到是否有任何配置可以获取微秒级别的纪元时间。
我正在尝试从纪元时间中找到我的算法所需的操作时间(以微秒为单位),但是我找不到任何方法来实现这一点。如果有任何文献/链接将不胜感激。
非常感谢您的帮助!
英文:
Is it possible to obtain epoch time in microseconds?
I am unable to find if there is any configuration to apply to get epoch time in microseconds.
I am trying to find operational time required by my algorithm in microseconds from epoch however I cannot find any way to do this. Any literature/Links would be appreciated.
Thanks a lot for helping out!!
答案1
得分: 1
std::chrono::time_point
的默认构造函数生成一个与时代的开始对应的时间点,因此选择一个时钟,使用now()
获取当前时间,减去默认构造的时间点并转换为微秒:
#include <iostream>
#include <chrono>
int main()
{
using namespace std::chrono_literals;
using Clock = std::chrono::system_clock;
std::cout << "Epoch time " << (Clock::now() - Clock::time_point{}) / 1us << "us\n";
}
英文:
The default constructor of std::chrono::time_point
produces a time point corresponding to the start of the epoch, so choose a clock, get the current time using now()
, subtract the default-constructed time point and convert to microseconds:
#include <iostream>
#include <chrono>
int main()
{
using namespace std::chrono_literals;
using Clock = std::chrono::system_clock;
std::cout << "Epoch time " << (Clock::now() - Clock::time_point{}) / 1us << "us\n";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论