Measure time problem

Hello all,
I want to measure running time in differemt part of my program, therefore I build a class like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
class MyTimer {
	clock_t begin_time, end_time;
public:
	void MyTimerStart() {
		begin_time = clock();
	};
	string MyElapseTime() {
		return to_string(float(end_time - begin_time) / CLOCKS_PER_SEC);
	};
	void MyTimerEnd() {
		end_time = clock();
	};
};


so that I can create timer for different parts/usage
e.g. MyTimer OverallTime, LoopTime;

However, when I use the code like this:
1
2
3
4
5
6
7
8
9
int main() {
	MyTimer OverallTime;
	OverallTime.MyTimerStart();

	// do something
	Sleep(3000);
	OverallTime.MyTimerEnd();
	cout << "Elapse time:" << OverallTime.MyElapseTime()<< endl;
}


It give:

Elapse time:15152.1


But if I replace
 
float(end_time - begin_time) / CLOCKS_PER_SEC

with
 
float(clock() - begin_time) / CLOCKS_PER_SEC

it gives:

Elapse time:3.045000

Which is correct! Why end_time not work ???

Regds
LAM Chi-fung
Last edited on
What you've shown suggests you had an error in the first test. end_time was used uninitialized. You need to run the program that shows the problem to confirm it happens and then copy and paste exactly that code completely in one piece so we can see exactly what you ran and can try it ourselves.
@tpb:
Yes, I extract code from my project...
Here is the complete class of MyTimer
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
class MyTimer {
	clock_t begin_time, end_time;
public:

	static string GetCurrentTime() {
		time_t     now = time(0);
		struct tm  tstruct;
		char       buf[80];
		tstruct = *localtime(&now);
		strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);

		return buf;
	}
	string MyTimerStart() {
		begin_time = clock();

		return GetCurrentTime();
	};
	string MyElapseTime() {
		return to_string(float(end_time - begin_time) / CLOCKS_PER_SEC);
	};
	string MyTimerEnd() {
		end_time = clock();

		return GetCurrentTime();
	};
};


What I am doing in my project is:
1
2
3
4
5
6
7
	MyTimer OverallTime;
	OverallTime.MyTimerStart();
	// do something
	Sleep(3000);
	cout << "Program finished at " << OverallTime.MyTimerEnd() << endl
		<< "elapsed time: " << OverallTime.MyElapseTime() << endl;


Seems that compiler think in different way as human (of course) ~_~
I expect the cout code will invoke MyTimerEnd() first and then MyElapseTime(). but compiler not think in that way.
When I change to:
1
2
3
4
5
6
7
	MyTimer OverallTime;
	OverallTime.MyTimerStart();
	// do something
	Sleep(3000);
	cout << "Program finished at " << OverallTime.MyTimerEnd() << endl;
	cout << "elapsed time: " << OverallTime.MyElapseTime() << endl;

everything works fine.

Regds
LAM Chi-fung
To avoid that the execution of cout << "Program finished at " is being included in your timings I think you should stop the timer on its own line, before using cout.

Also note that clock() tries to estimate the time that your CPU spends executing your program so the 3 seconds that your program spends sleeping will probably not be included in the timings.
Last edited on
@Peter87, yes, I do exactly what you said to solve the problem.
Topic archived. No new replies allowed.