This is the code I use:
const auto start = std::chrono::steady_clock::now(); // Your code here. const auto end = std::chrono::steady_clock::now(); std::chrono::duration<double> elapsed = end - start; std::cout << "Time in seconds: "<< elapsed.count() << '\n';
You don't want to use std::chrono::system_clock
because it is not monotonic! If the user changes the time in the middle of your code your result will be wrong - it might even be negative. std::chrono::high_resolution_clock
might be implemented using std::chrono::system_clock
so I wouldn't recommend that either.
This code also avoids ugly casts.