4

How can I keep track of time in seconds and milliseconds since my game/program started? I can use the clock() function but I hear it is not that accurate. Is there a better way?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
user2204292
  • 181
  • 1
  • 4
  • 10
  • 12
    Use `std::chrono::high_resolution_clock`. –  Oct 27 '16 at 14:08
  • http://stackoverflow.com/questions/275004/timer-function-to-provide-time-in-nano-seconds-using-c – Rob Oct 27 '16 at 14:09
  • 4
    C or C++? Choose one. They are different languages and the answer will be very different. – Sami Kuhmonen Oct 27 '16 at 14:09
  • Game time or wall clock? I mean, if I stop the game for half an hour, the time you want is `t` or `t-15m`? – skypjack Oct 27 '16 at 14:13
  • see the following answer about clock and precision [enter link description here](http://stackoverflow.com/questions/1487695/c-cross-platform-high-resolution-timer) – cristallo Oct 27 '16 at 14:17
  • Platform? If Windows, call `GetTickCount()`at the start of your frame, and use the result for the remainder of the frame. Whichever timer you use, don't read it directly after the start of the frame. You want everything that needs the time to use the same value. – 3Dave Oct 27 '16 at 14:21

3 Answers3

8

You can use the chrono library in C++ Here is a code sample:

#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
int main() {                         
    high_resolution_clock::time_point t1 = high_resolution_clock::now(); 
    high_resolution_clock::time_point t2 = high_resolution_clock::now();  
    duration<double> time_span = duration_cast<duration<double>>(t2 - t1);      
    cout << time_span.count() << " seconds\n";
    return 0; 
} 

Note that this c++11, so to compile it you should use the flag -std=c++11

$ g++ -std=c++11 test.cpp -o test

This exact piece of code gave 4e-07 seconds on my PC.

Hope that helps.

Pranshu Gupta
  • 634
  • 9
  • 13
  • 6
    This is the reason `auto` is essential for C++! – DeiDei Oct 27 '16 at 14:26
  • 3
    *"So, it is pretty much very precise."* - You are confusing *"precision"* with *"accuracy"*. What good is nanosecond precision, when all you get is millisecond accuracy? – IInspectable Oct 27 '16 at 17:48
6

A cross-platform and easy solution would be to use the chrono library

Example:

#include <iostream>
#include <chrono>

void gameFunction()
{
    // start()
    // end()
}

int main()
{
    auto t1 = std::chrono::high_resolution_clock::now();
    gameFunction();
    auto t2 = std::chrono::high_resolution_clock::now();

    auto elapsed_time = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();

    std::cout << elapsed_time << std::endl;
    return 0;
}

Finally note that you will need a at least C++11 for this to work, so set the -std= flag to at least c++11. For example:

g++ -std=c++11 game.cpp -o game
Makaronodentro
  • 907
  • 1
  • 7
  • 21
  • C++ _MAY_ reorder those clock statements to be meaningless to you. See [Enforcing statement order in C++](https://stackoverflow.com/questions/37786547/enforcing-statement-order-in-c) for more. – Dúthomhas Apr 01 '19 at 10:41
1

I highly recommend you look at Handmade Hero. Casey records creating an entire game in a series of video episodes. In one of the early episodes he discusses determining wall-clock time on windows using QueryPerformanceCounter and QueryPerformanceFrequency

He also discusses using cpu cycle counts although those are useful only assuming a constant processor clock speed.

He talks about these issues again in later episodes: https://hero.handmade.network/episode/game-architecture/day113 and https://hero.handmade.network/episode/game-architecture/day177

Since you are looking for a solution in a game loop, these videos will probably be of interest at least even if you use the cross-platform solution from @Pranshu. You are likely ok for a game using a platform dependent method to get a more accurate clock.

I'll point out that high_resolution_clock provides the highest resolution clock available by the system that the library knows about so it might not be any more accurate than using system_clock or steady_clock. (It uses steady_clock on my OSX box.)

Phil
  • 1,226
  • 10
  • 20