I have several questions about new <chrono> header in C++ 11. Using Windows 7, Visual Studio 2012.
Looking at the example http://en.cppreference.com/w/cpp/chrono
#include <iostream> #include <chrono> #include <ctime> int fibonacci(int n) { if (n < 3) return 1; return fibonacci(n-1) + fibonacci(n-2); } int main() { std::chrono::time_point<std::chrono::system_clock> start, end; start = std::chrono::system_clock::now(); int result = fibonacci(42); end = std::chrono::system_clock::now(); int elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds> (end-start).count(); std::time_t end_time = std::chrono::system_clock::to_time_t(end); std::cout << "finished computation at " << std::ctime(&end_time) << "elapsed time: " << elapsed_seconds << "s\n"; } Possible output
finished computation at Sat Jun 16 20:42:57 2012 elapsed time: 3s - I have noticed that example uses
std::chrono::system_clock::now();does it mean it can be used to measure only elapsed time and not the CPU time ??? And if I want to measure CPU time, what Clocks should I use ? - Notice that
elapsed time: 3sis output is rounded to whole integer. Is there way to make it more granulated?
seconds. Usemillisecondsand voila, more granulated! Heck, usenanosecondsif you want.endto meanstd::end. It might have been GCC 4.7.1, but I could be wrong. I know the latest version of GCC no longer does that, but it's something to watch out for if you have other compilers that will use this.<chrono>is a header (and namespace), not a class. I corrected that for you.