Differences
This shows you the differences between two versions of the page.
| — |
c_11:examples:chrono_stop_watch.cc [2017/02/25 18:01] (current) mike created |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | <file c++ chrono_stop_watch.cc> | ||
| + | #include <iostream> | ||
| + | #include <chrono> | ||
| + | #include <ctime> | ||
| + | |||
| + | long fibonacci(unsigned n) | ||
| + | { | ||
| + | if (n < 2) return n; | ||
| + | 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(); | ||
| + | std::cout << "f(42) = " << fibonacci(42) << '\n'; | ||
| + | end = std::chrono::system_clock::now(); | ||
| + | |||
| + | std::chrono::duration<double> elapsed_seconds = end-start; | ||
| + | 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.count() << "s\n"; | ||
| + | } | ||
| + | </file> | ||