0

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?

motam79
  • 3,542
  • 5
  • 34
  • 60
  • What's wrong with using `std::chrono`? Perhaps / maybe / probably the boost implementation is just inefficient, doubt there's anything you can do. – Paul Sanders Jul 08 '18 at 16:13
  • "Is there a better(faster) way to get the time since epoch" - yeah, use `std::chrono`. – Jesper Juhl Jul 08 '18 at 16:31
  • Boost usually has experimental implementations that go in the standard library later, so that's why it might be slower. – MivVG Jul 08 '18 at 18:12

1 Answers1

1

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.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60