#include#include int main() { auto start = std::chrono::high_resolution_clock::now(); // ... code to measure ... auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration elapsed = end - start; std::cout << "Elapsed time: " << elapsed.count() << " seconds\n"; }
#includeIn this code example, we are measuring the time it takes to execute two threads simultaneously. We declare a `start` variable and set it to the current time using `std::chrono::high_resolution_clock::now()`. We then create two threads with the function `do_something()` and wait for them to finish using `join()`. After the threads have finished, we declare an `end` variable and set it to the current time as well. We calculate the elapsed time by subtracting `start` from `end` and storing the result in a `std::chrono::duration#include void do_something() { for (int i = 0; i < 100000; i++) { // ... } } int main() { auto start = std::chrono::high_resolution_clock::now(); std::thread t1(do_something); std::thread t2(do_something); t1.join(); t2.join(); auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration elapsed = end - start; std::cout << "Elapsed time: " << elapsed.count() << " seconds\n"; }