Skip to content

Instantly share code, notes, and snippets.

@jimdiroffii
Last active November 2, 2023 00:30
Show Gist options
  • Save jimdiroffii/b473b9f46c5b7c472581903ff4f24291 to your computer and use it in GitHub Desktop.
Save jimdiroffii/b473b9f46c5b7c472581903ff4f24291 to your computer and use it in GitHub Desktop.
Calculate elapsed time in C++ program using chrono with high resolution and precision
#include <iostream> // cout
#include <iomanip> // fixed, setprecision
#include <chrono> // time library
{
auto startTime = std::chrono::high_resolution_clock::now();
/* work */
auto endTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = endTime - startTime;
std::cout << "Time Elapsed: " << std::fixed << std::setprecision(6) << duration.count() << " seconds\n";
}
@jimdiroffii
Copy link
Author

Example output: Time Elapsed: 0.001729 seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment