4

What is the simplest and cleanest way to get the time since execution of the program (with milliseconds precision) in C++ ?

I am making a wave interference simulator to produce Lissajous curves in C++. It requires the time since execution of the program (with at least milliseconds precision) to function. I can't seem to find any clean and simple way to do it after a bit of research.
All <chrono> functions seem very confusing to me. Similar questions on here on Stack Overflow seem to be either unrelated, confusing (to me) or inapplicable for my situation. I tried using functions from <time.h>, only to discover that they have precision upto seconds only.
I am running Windows 7 x64. The program need not be platform independent as it's for personal use. Any help is greatly appreciated.
Thank you!

AvZ
  • 997
  • 3
  • 14
  • 25

1 Answers1

7

The new <chrono> functions take a little getting used to but they make things fairly easy when you get to know how they work.

Your problem could be solved like this for example:

#include <chrono>
#include <thread>
#include <iostream>

// for readability
using hr_clock = std::chrono::high_resolution_clock;
using hr_time_point = hr_clock::time_point;
using hr_duration = hr_clock::duration;
using milliseconds = std::chrono::milliseconds;

int main()
{
    // note the program start time
    hr_time_point prog_start = hr_clock::now();

    // do stuff
    std::this_thread::sleep_for(milliseconds(1000));

    // find the duration
    hr_duration d = hr_clock::now() - prog_start;

    // cast the duration to milliseconds
    milliseconds ms = std::chrono::duration_cast<milliseconds>(d);

    // print out the number of milliseconds
    std::cout << "time passed: " << ms.count() << " milliseconds.\n";
}

For convenience you could create a function to return the time since that function was last called:

milliseconds since_last_call()
{
    // retain time between calls (static)
    static hr_time_point previous = hr_clock::now();

    // get current time
    hr_time_point current = hr_clock::now();

    // get the time difference between now and previous call to the function
    milliseconds ms = std::chrono::duration_cast<milliseconds>(current - previous);

    // store current time for next call
    previous = current;

    // return elapsed time in milliseconds
    return ms;
}

int main()
{
    since_last_call(); // initialize functions internal static time_point

    // do stuff
    std::this_thread::sleep_for(milliseconds(1000));

    milliseconds ms = since_last_call();

    // print out the number of milliseconds
    std::cout << "time passed: " << ms.count() << " milliseconds.\n";
}
Galik
  • 47,303
  • 4
  • 80
  • 117
  • I don't understand how the `// do stuff` and `// cast the duration to milliseconds` lines work. Also, is it possible to make it into a function which returns the time till the function was called instead? Thanks. – AvZ Mar 08 '15 at 15:51
  • @AvZ The // do stuff line is a placeholder for whatever it is your program will be doing while the timer is running. The duration cast converts the duration from the clock's natural units into milliseconds. Different clocks count at different rates so casting to a normal rate ratio is needed to get standard units like milliseconds/seconds etc – Galik Mar 08 '15 at 16:02