Timing program execution?

What is a good way to do this?

Right now, I'm doing this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <ctime>
#include <iomanip>

using namespace std;

int main()
{
	double timer01 = clock();
	//stuff
	double timer02 = clock();
	cout.precision(4);
	cout << "Program run-time is " << fixed << (timer02 - timer01) / CLOCKS_PER_SEC << " seconds." << endl;
	return 0;
}

But I always seem to end up with 0.
Clock() returns the time sense the beginning of the program execution. Should only need one call to it
I see... but how does that explain why I'm getting 0?

I find it hard to believe that the code in "stuff" is running so fast that the program can't even time it.
I find it hard to believe that the code in "stuff" is running so fast that the program can't even time it.
It's possible, put a Sleep between the timings and you will see it works.
http://www.physicsforums.com/showthread.php?t=224989
third post shows how to use it correctly.
multiply your (timer02 - timer01) with 1000
$ time ./program.bin


put a Sleep between the timings and you will see it works.
A sleep() should block the process.
As clock() measures the time in execution, it shouldn't change.
Topic archived. No new replies allowed.