-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunTime.cpp
More file actions
35 lines (27 loc) · 896 Bytes
/
runTime.cpp
File metadata and controls
35 lines (27 loc) · 896 Bytes
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
32
33
34
35
/*
The chrono library in C++ is used to time the scriptor infer time in units if milliseconds.
This small example is used to time a recursive factorial script.
*/
#include <iostream>
#include <chrono>
int factorial(int num)
{
if(num==1 || num==0)
return 1;
else
return num*factorial(num-1);
}
int main() {
// Measure time taken for goodnight1():
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
std::cout << factorial(5) << "\n";
std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> time_span = end - start;
// Print time taken for goodnight1():
std::cout << "Time taken for factorial(5): " << time_span.count() << " milliseconds.\n\n";
}
/*
Output :
120
Time taken for factorial(5): 0.040379 milliseconds.
*/