A simple example of a print time
#include <iostream> #include <chrono> #include <thread> int main() { using namespace std::literals::chrono_literals; auto start = std::chrono::high_resolution_clock::now(); std::this_thread::sleep_for(1s); auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration<float> duration = end - start; std::cout << duration.count() << "s" << std::endl; std::cin.get(); }
After performing output display 1s.
Another use case is this more
#include <iostream> #include <chrono> #include <thread> struct Timer { std::chrono::time_point<std::chrono::steady_clock> start, end; std::chrono::duration<float> duration; Timer() { start = std::chrono::high_resolution_clock::now(); } ~Timer() { end = std::chrono::high_resolution_clock::now(); duration = end - start; float ms = duration.count() * 1000.0f; std::cout << "Timer took " << ms << "ms" << std::endl; } }; void Function() { Timer timer; for (int i = 0; i < 100; i++) std::cout << "Hello" << std::endl; } int main() { Function(); std::cin.get(); }
We create a Timer of the struct, 100 secondary calculate how much time print.
We can put the std :: endl remove each print
for (int i = 0; i < 100; i++) std::cout << "Hello\n";
In this way, to print, can significantly improve the time, because std :: endl always takes time.
In short, this is a very rough example of the calculation of the time, but you can help to understand.