Timing a Loop

So I had to modify a program so that the loop is timed and it's printed out. But I don't get the right timing on the clock. What's wrong??
1st is an h file, 2nd a cpp file and third is implementation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #ifndef CLOCK_UTILS_H
#define CLOCK_UTILS_H

#include <ctime>
using namespace std;

struct Duration {
	int hours;
	int minutes;
	int seconds;
	int tics;
};

int buildDuration (clock_t t);
string asString (Duration x);


#endif 


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
#include <ctime>
#include <sstream>
#include "clock_utils.h"
using namespace std;
const int SECONDS_PER_MINUTE = 60, MINUTES_PER_HOUR = 60;


int buildDuration (clock_t t) {
	Duration x;
	x.tics = t % CLOCKS_PER_SEC;
	x.seconds = t / CLOCKS_PER_SEC;
	x.minutes = x.seconds / SECONDS_PER_MINUTE;
	x.seconds %= SECONDS_PER_MINUTE;
	x.hours = x.minutes / MINUTES_PER_HOUR;
	x.minutes %= MINUTES_PER_HOUR;
	
	return (x.tics && x.seconds && x.minutes && x.hours);
}

string asString (Duration x) {
    stringstream t, s, h, m;
    string col = ":";
    t << x.tics;
    s << x.seconds;
    m << x.minutes;
    h << x.hours;
    string b = (h.str() + col + m.str() + col + s.str() + col + t.str());
    
    return b;
}


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
#include <iostream>
#include "clock_utils.h"
using namespace std;

int main() {
    Duration x;
	cout << "Clock ticks per second: " << CLOCKS_PER_SEC << endl;
    
	clock_t start_time = clock();
	for (int i = 0; i < 1000000; i++)
		i / 17;
	clock_t end_time = clock();
	clock_t duration = end_time - start_time;
    
	cout << "Single loop 0 to 1,000,000: " << x.hours << ":" << x.minutes << ":" << x.seconds << ":" << x.tics << endl;
    
	start_time = clock();
	for (int i = 0; i < 1000; i++)
		for (int j = 0; j < 1000000; j++)
			i / 17;
	end_time = clock();
	duration = end_time - start_time;
    
	cout << "Nested loops 0 to 1,000 / 0 to 1,000,000: " << x.hours << ":" << x.minutes << ":" << x.seconds << ":" << x.tics << endl;
    
	return 0;
}

Hi,

You are just declared a varible for Duration, you are not filling the data variables.
Duration x;


Try with revised code given blow
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
Duration buildDuration (clock_t t) {
	Duration x;
	x.tics = t % CLOCKS_PER_SEC;
	x.seconds = t / CLOCKS_PER_SEC;
	x.minutes = x.seconds / SECONDS_PER_MINUTE;
	x.seconds %= SECONDS_PER_MINUTE;
	x.hours = x.minutes / MINUTES_PER_HOUR;
	x.minutes %= MINUTES_PER_HOUR;
	
	return x;
}


int main() {
        Duration x;
	cout << "Clock ticks per second: " << CLOCKS_PER_SEC << endl;
    
	clock_t start_time = clock();
	for (int i = 0; i < 1000000; i++)
		i / 17;
	clock_t end_time = clock();
	clock_t duration = end_time - start_time;

	x=buildDuration(duration);
    
	cout << "Single loop 0 to 1,000,000: " << x.hours << ":" << x.minutes << ":" << x.seconds << ":" << x.tics << endl;
    
	start_time = clock();
	for (int i = 0; i < 1000; i++)
		for (int j = 0; j < 1000000; j++)
			i / 17;
	end_time = clock();
	duration = end_time - start_time;

	x=buildDuration(duration);    
	cout << "Nested loops 0 to 1,000 / 0 to 1,000,000: " << x.hours << ":" << x.minutes << ":" << x.seconds << ":" << x.tics << endl;
    
	return 0;
}


Let me know if you have any concers with the above code.
Thanks! It worked! The buildDuration function was supposed to return a Duration object and I misread that on the homework.
Again, thank you!
Topic archived. No new replies allowed.