-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathExecution time.cpp
31 lines (21 loc) · 961 Bytes
/
Execution time.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// This example displays information about the execution time of a function call:
#include <iostream>
#include <chrono> // header file
long fibonacci(unsigned n)
{
if (n < 2) return n;
return fibonacci(n-1) + fibonacci(n-2);
}
int main()
{
auto start = std::chrono::steady_clock::now(); // start from here(from the where you want to show execution time)
std::cout << "f(2) = " << fibonacci(2) << '\n';
auto end = std::chrono::steady_clock::now(); // end here(at the end of function)
std::chrono::duration<double> elapsed_seconds = end-start; // this is calculate duration of execution time of the function
std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n"; // print the exicution time
}
//The chrono library defines three main types as well as utility functions and common typedefs.
// clocks
// time points
// durations
// for more detail--> visit: https://en.cppreference.com/w/cpp/chrono