3

Probably duplicate, but What is gettimeofday() equivalent in c++11 ?

I am trying to get 64 bit timestamp with microseconds, similar to Java / Python.

Nick
  • 9,962
  • 4
  • 42
  • 80

2 Answers2

5

Use std::chrono::system_clock::now().

UPDATE

You can check the necessary minimal precision by this static assert:

static_assert (std::ratio_less_equal<std::chrono::system_clock::duration::period,
    std::ratio<1,100> >::value, "");
wilx
  • 17,697
  • 6
  • 59
  • 114
  • Are we sure it is *always* offering a microsecond resolution? Can't a C++11 compliant implementation only give a decisecond-accurate system clock? – Basile Starynkevitch Nov 09 '15 at 12:49
  • @BasileStarynkevitch - decisecond good for me. I don't think Java do microseconds. – Nick Nov 09 '15 at 12:51
  • @Nick I've no idea what Java's resolution is but you seemed to think Java measures in microseconds when you posted your question: _trying to get 64 bit timestamp with microseconds, similar to Java / Python._ – mah Nov 09 '15 at 12:57
  • 2
    there is also a `high_resolution_clock` which might or might not be the same as `system_clock` – sp2danny Nov 09 '15 at 13:00
2

To get the highest resolution supported by your system, use std::high_resolution_clock::now. The high-resolution clock may be an alias for std::chrono::system_clock.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214