How to use the timer in c++

Hey guys so i have to time how long each of my functions take to run and am stuck i know i have to use the time.h but i don't know how to start and end it heres my code i have to time the fibonacci number using recursion and iteratively. I have to time them each to see which one is faster to calculate odd number from 1-15. I have all the code written just need to implement the timer

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <time.h>

using namespace std;

int recFib(int n);

int recFib(int n){
	
	if (n<2) {
		return 1;
	}
	else {
		return (recFib(n-2) + recFib(n-1));
	}
	
}


int itFib(int n);

int itFib(int n){
	
	int m=0;
	int k=1;
	int num;
	
for (int i=0;i<=n;i++) {
		
		num=m+k;
		m=k;
		k=num;
	}
	return m;
}


int main() {

int result, result2;

cout << "Iterative Function" << endl;

// have to time this 
for(int i=1;i<=15;i=i+2){
	
	result= itFib(i);
	cout << result <<  endl;
}

cout << endl;
cout << "Recursion Function"

// have to time this 
for (int i=1;i<=15;i=i+2) {
	
	result = recFib(i);
	cout << result2 << endl
	
}

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <ctime>

int main()
{
    // http://www.cplusplus.com/reference/ctime/clock/
    const std::clock_t start = std::clock() ;

    {
        // put the code to be timed here
    }

    const std::clock_t end = std::clock() ;

    std::cout << "processor time: " << double(end-start) * 1000 / CLOCKS_PER_SEC
               << " milliseconds\n" ;
}
Topic archived. No new replies allowed.