I would like to have a function to get time since epoch in ns. I was having a solution using boost::ptime
:
auto cur_time = ptime(day_clock::universal_day(), return microsec_clock::universal_time().time_of_day());
return (cur_time - date(1970,1,1)).total_microseconds();
I also tested the solution using std::chrono:
return std::chrono::duration_cast< std::chrono::microseconds >(std::chrono::system_clock::now().time_since_epoch()).count();
The boost solution is 10 times slower than the std::chrono solution. (150ns vs 1500ns on my Linux machine). Is there a better(faster) way to get the time since epoch in Boost::datetime that I am missing?
Boost is flexible cross platform code, libstdc++ (presumably the standard library you are using) was written together with your compiler and is newer code than boost::chrono so is more likely to have the most efficient implementation possible.
I'd always recommend using the standard library implementations over boost where possible and where the standard library implementation is complete. Its generally reasonably easy to switch between the implementations using a single #ifdef
.