calculate time difference cpp 1

calculate time difference cpp


auto t_start = std::chrono::high_resolution_clock::now();
// the work...
auto t_end = std::chrono::high_resolution_clock::now();

double elapsed_time_ms = std::chrono::duration(t_end-t_start).count();

Here is what the above code is Doing:
1. We create a variable t_start that stores the current time.
2. We do the work we want to time.
3. We create a variable t_end that stores the current time.
4. We create a variable elapsed_time_ms that stores the difference between t_end and t_start.

The std::chrono::duration class is a template class that represents a time duration. The first template parameter is the type of the duration (in this case, double), and the second template parameter is the unit of the duration (in this case, milliseconds).

The std::chrono::duration::count() method returns the duration in the specified unit.

Similar Posts