0

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
    Yes, it's possible. See [](https://en.cppreference.com/w/cpp/header/chrono). – molbdnilo May 26 '23 at 05:23
  • 1
    Does this answer your question? [Print current system time in nanoseconds using c++ chrono](https://stackoverflow.com/questions/27677964/print-current-system-time-in-nanoseconds-using-c-chrono) – vvv444 May 26 '23 at 05:57

1 Answers1

1

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";
}
fabian
  • 80,457
  • 12
  • 86
  • 114