Calculate how long a program took to run?

Hello All. I have this simple program here and I want to know how long it took run. Would I use system clock or something out of the time.h?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
        int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

	for( int i = 0; i < 9; i++ )
		cout << a[i] << " ";
	                                                                     
	cout << endl;
	system("pause");
	return 0;
}


The output is: 0 1 2 3 4 5 6 7 8
and I want to add: And it took ????? seconds to run.

Thanks for the help,
pooshi
A program that small will almost always run < 1s, unless there is resource contention on a multi-tasking OS.

I wrote a brief program a month or so ago that profiled the time required to compute an object's relativistic mass(1 sqrt, one division, 3 multiplies) 10,000 times. It ran well under a second.

You'll need a high resolution timer and the functionality is OS dependent. For Windows, look up QueryPerformanceFrequency and QueryPerformanceCounter.

For Linux look at clock.

If you're running Windows, add this to your program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// variables to add
	_int64 countspersec, curr_time, prev_time;
	double secpercount;


// Get the counts per second for the HR timer and calculate the number of seconds one count corresponds to.
	QueryPerformanceFrequency((LARGE_INTEGER*)&countspersec);
	secpercount = 1.0 / (double)countspersec;

// sandwich your code between these two calls
	QueryPerformanceCounter((LARGE_INTEGER*)&prev_time);
	QueryPerformanceCounter((LARGE_INTEGER*)&curr_time);


// reporting code
	cout << "Counts it took to run: " << curr_time - prev_time << endl;
	cout << "Seconds it took to run: " << (curr_time - prev_time) * secpercount << endl;

Last edited on
Roberts,

I did as instructed and I had to add a windows header file. I got a ton of errors when I ran it. I working on a system with Windows 7 and using VS 2010.
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
#include <iostream>
#include <WinBase.h>
using namespace std;

int main()
{                                                                                                                                                                      
	
	_int64 countspersec, curr_time, prev_time;
	double secpercount;

	QueryPerformanceCounter( ( LARGE_INTEGER* ) &countspersec );
	secpercount = 1.0 / ( double ) countspersec; 

	QueryPerformanceCounter( ( LARGE_INTEGER* ) &prev_time );
	QueryPerformanceCounter( ( LARGE_INTEGER* ) &curr_time );


	int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

	for( int i = 0; i < 9; i++ )
		cout << a[i] << " ";

	cout << "Counts it took to run: " << curr_time - prev_time << endl;
	cout << "Seconds it took to run: " << (curr_time - prev_time) * secpercount << endl;
	                                                                     
	cout << endl;
	system("pause");
	return 0;
}

1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winbase.h(243): error C2146: syntax error : missing ';' before identifier 'Internal'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winbase.h(243): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winbase.h(243): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

These are just a couple of errors I got. Most of the rest say pretty much the same thing.
Any ideas?

Thanks,
Bob
Do you have the win32 SDK installed?

Also, when I said:


// sandwich your code between these two calls
QueryPerformanceCounter((LARGE_INTEGER*)&prev_time);
QueryPerformanceCounter((LARGE_INTEGER*)&curr_time);


You should have:
1
2
3
4
5
6
7
8
9
	QueryPerformanceCounter( ( LARGE_INTEGER* ) &prev_time );



	int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

	for( int i = 0; i < 9; i++ )
		cout << a[i] << " ";
	QueryPerformanceCounter( ( LARGE_INTEGER* ) &curr_time );

Last edited on
And the first function call with countspersec should be QueryPerformanceFrequency.
Also, don't include winbase.h, rather, include windows.h.

The error messages are flagging:

1
2
3
4
5
6
7
8
9
10
11
12
13
typedef struct _OVERLAPPED {
    ULONG_PTR Internal;
    ULONG_PTR InternalHigh;
    union {
        struct {
            DWORD Offset;
            DWORD OffsetHigh;
        } DUMMYSTRUCTNAME;
        PVOID Pointer;
    } DUMMYUNIONNAME;

    HANDLE  hEvent;
} OVERLAPPED, *LPOVERLAPPED;


Which tells me it's not recognizing ULONG or PTR as valid typedefs. The online doc at MS says the functions are defined in WinBase.h, but that you should include windows.h.



Thanks guys. The windows.h header file did the trick.
The output is ridiculous:
0 1 2 3 4 5 6 7 8
Counts it took to run: 8512
Seconds it took to run: 0.00344921.

Thanks again. Never knew you could do that,
Bob
That took a surprisingly long time to run. It must be the output functionality that is slowing it down.

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
#include <Windows.h>
#include <iostream>
#include <cmath>
#include <iostream>

using namespace std;

int main( int argc, char *argv[] )
{
// variables to add
	_int64 countspersec, curr_time, prev_time;
	double secpercount;
	double rel_mass, rest_mass = 5000000000, v = 100000, c = 299792458;

// Get the counts per second for the HR timer and calculate the number of seconds one count corresponds to.
	QueryPerformanceFrequency((LARGE_INTEGER*)&countspersec);
	secpercount = 1.0 / (double)countspersec;

// sandwich your code between these two calls
	QueryPerformanceCounter((LARGE_INTEGER*)&prev_time);
	for( int i = 0; i < 10000; i++ )
		rel_mass = sqrt( rest_mass/(sqrt(1-(v*v)/(c*c))));

	QueryPerformanceCounter((LARGE_INTEGER*)&curr_time);


// reporting code
	cout << "Counts it took to run: " << curr_time - prev_time << endl;
	cout << "Seconds it took to run: " << (curr_time - prev_time) * secpercount << endl;
	cin.get();

}



Counts it took to run: 788
Seconds it took to run: 0.000268996


And I even have the equation wrong (superfluous sqrt in there).
Last edited on
Topic archived. No new replies allowed.