I need a way to get time for calculating the delta time.

I see that OpenGL and GLFW have methods for it, but unfortunately I can't use them since I need to make server software too.

What is the best way to get the time, without using OpenGL/GLFWs methods? Either in milliseconds or microseconds.

Note: Must be cross platform, preferably in the default c++ 11 library or a part of the boost library.

  • 3
    \$\begingroup\$ Use the chrono import. \$\endgroup\$ – Lolums Nov 28 '15 at 15:25
  • \$\begingroup\$ Does it matter to you that basically every possible option will use different platform/OS specific methods under the hood (and thus may be subject to different drifts and inaccuracies), or are you just looking for a way to avoid having to write such an abstraction yourself? \$\endgroup\$ – user1430 Nov 28 '15 at 15:44
  • \$\begingroup\$ @JoshPetrie No, doesn't matter, as long as the differences are very minor \$\endgroup\$ – KaareZ Nov 28 '15 at 15:56
  • \$\begingroup\$ What is the connection between calculating the deltaTime and making server software? In other words: Why would the method for finding deltaTime affect you making a game server? \$\endgroup\$ – mythos Nov 28 '15 at 17:19
  • 1
    \$\begingroup\$ @sakul_ca I will need to calculate the delta time on the server too. Therefore I need a solution that doesn't depend on OpenGL or GLFW \$\endgroup\$ – KaareZ Nov 28 '15 at 17:23

The standard library header chrono (available since C++11) provides various clock and duration types you can use to compute time intervals (in theory -- see my footnote and the comments). You probably want steady_clock. Your periodic update method would look something like:

auto currentTime = std::chrono::steady_clock::now();
auto elapsed = previousTime - currentTime;

... do stuff with elapsed time...
float elapsed_f = elapsed.count();

previousTime = currentTime;

(where previousTime is a member or some other persistent storage for the time point). The subtraction of two time points will result in a duration object. This site also contains a more concrete example of using the clock mechanism.

If steady_clock isn't fine-grained enough for you, you can look at high_resolution_clock. However, it's important to test if the clock that backs the high resolution clock is monotonically increasing (check the is_steady member function), because it's not guaranteed to be so.

Note that as discussed in the comments, chrono has some issues which tend to make it less than ideal for games -- some of them are due to the wording of the standard providing lots of leeway, some of them are just down to poor implementations. Even though it would not be a great answer to your question, using platform-specific timer mechanisms (possibly wrapped up on your own abstraction) are likely the better solution to your problem.

| improve this answer | |
  • 3
    \$\begingroup\$ steady_clock is extremely unlikely to have the resolution for measuring ticks in games. You're more likely to want high_resolution_clock as you noted, except of course it's not guaranteed to be the highest resolution clock on the system (it might even just be an alias for system_clock or steady_clock). Your best bet - even at the end of 2015 - is still to use the platform-specific timers. \$\endgroup\$ – Sean Middleditch Nov 28 '15 at 18:47
  • \$\begingroup\$ Josh Petrie How can I convert auto to a primitive? @SeanMiddleditch I guess I could just go with the platform specific if there's no reliable cross-platform. Which should I use for windows? \$\endgroup\$ – KaareZ Nov 28 '15 at 19:22
  • \$\begingroup\$ @KareeZ: the usual go-to on Windows is QueryPerformanceCounter, which is how a "good" implementation of high_resolution_clock works under the hood (note: MSVC 2013 did not in fact have a good implementation, though MSVC 2015's is). \$\endgroup\$ – Sean Middleditch Nov 28 '15 at 19:37
  • \$\begingroup\$ @SeanMiddleditch That's fine, because I use VS15. Could you please quickly explain how I work with this 'auto' type? \$\endgroup\$ – KaareZ Nov 28 '15 at 19:58
  • \$\begingroup\$ @KareeZ: not in a comment, no, but you can Google it trivially. \$\endgroup\$ – Sean Middleditch Nov 28 '15 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.