Epoch time in microseconds 微秒级别的纪元时间

huangapple go评论80阅读模式
英文:

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 &lt;iostream&gt;
#include &lt;chrono&gt;

int main()
{
    using namespace std::chrono_literals;

    using Clock = std::chrono::system_clock;

    std::cout &lt;&lt; &quot;Epoch time &quot; &lt;&lt; (Clock::now() - Clock::time_point{}) / 1us &lt;&lt; &quot;us\n&quot;;
}

huangapple
  • 本文由 发表于 2023年5月26日 12:58:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定