Calculating Run Time

Hi, I am having a trouble with calculating the run time the purpose of the program is to create a const where the value is determined by reading the time when the program starts. i used ctime as the standard header. in another function it have 2 arrays that it loops 10,000 times multiplying each corresponding elements the array in a single loop. Which then i display the results and the elapse time. This is what I have so far. I am having trouble on getting the run time.

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
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
using namespace std;

class ArrayTime
{
	public:
		void loop();
		void setTime1(clock_t t3);
		void setTime2(clock_t t2);
		char getTime1();
		char getTime2();
	
private:
		clock_t t3, t2;

};

int main()
{
	ArrayTime t1;
	t1.setTime1();
	t1.loop();
	t1.setTime2();

	float diff = ((float)t1.getTime2() - (float)t1.getTime1())/10000.0F;
	cout << "The run time is: " << diff << "\n";
	cin.ignore();
}



clock_t ArrayTime::setTime1(clock_t t3)
{
	t3 = clock();
}

clock_t ArrayTime::setTime2(clock_t t2)
{
	t2 = clock();
}

void ArrayTime::getTime1()
{
	return t3;
}

void ArrayTime::getTime2()
{
	return t2;
}

void ArrayTime::loop()
{
	double number1 = 100;
	double number2 = 10099;	
	double array1[10000];
	double array2[10000];

	for(int i = 0; i <10000; i++)
	{
		array1[i]= number1;
		array2[i] = number2;

		const double result = array1[i]* array2[i];
		
		number1++;
		number2--;
	}
I am having trouble on getting the run time.

What's that supposed to mean?

Your setTime/getTime prototypes are nonsense and they don't even match.
Focus a bit more on what you're doing.

Please also note that an optimizing compiler will optimize the entire loop() function away, since it doesn't actually do anything with observable effects. So your elapsed time will always be zero or close to that.
Last edited on
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
64
65
66
67
/*
 Title: TMA3Question1.cpp
 Description:
 Date: January 8, 2012
 Author: Dj Sicoli - 3033420
 Version: 1.0
 Copyright: 2012 Dj Sicoli- 3033420
*/

/*
 DOCUMENTATION

 Program Purpose:


 Classes: none

 Variables:


*/

#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
using namespace std;

class ArrayTime
{
	public:
		void loop();
};

int main()
{
	ArrayTime t1;
	clock_t t2,t3;
	t2 = clock(); 
	t1.loop();
	t3 = clock();

	float diff = ((float)t3 - (float)t2)/10000.0F;
	cout << "The run time is: " << diff << "\n";
	cin.ignore();
}

void ArrayTime::loop()
{
	double number1 = 100;
	double number2 = 10099;	
	double array1[10000];
	double array2[10000];

	for(int i = 0; i <10000; i++)
	{
		array1[i]= number1;
		array2[i] = number2;

		const double result = array1[i]* array2[i];
		
		number1++;
		number2--;
	}


}


the difference is 0 every time i run it
Yeah, see my edit to the previous post.
You can e.g. sum up "result", return it and then print it in main().
That should prevent the removal of the entire function.
Even so, 10000 iterations should take <1 ms, so your result might still be 0.
Last edited on
Do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void ArrayTime::loop()
{
        double number1 = 100;
        double number2 = 10099;
        double array1[10000];
        double array2[10000];

        for(int i = 0; i <10000; i++)
        {
                array1[i]= number1;
                array2[i] = number2;

                const double result = array1[i]* array2[i];

                number1++;
                number2--;
        }
        int i(1000000000);
        while(i--);     // ----------> Just to make it take some time
}

And change to
 
float diff = (t1.getTime2() - t1.getTime1())/float(CLOCKS_PER_SEC);

I get:
 
cout << "The run time is " << t1.getTime2() << "-" << t1.getTime1() << "=" <<diff << "\n";

Output:
The run time is 2720000-0=2.73

I did make other changes to get it to compile.
Last edited on
If optimizations are enabled, the compiler should get rid of the waiting loop too. gcc does, in any case.
so this is what my code looks like, i finally got an answer for results.

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
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
using namespace std;

class ArrayTime
{
	public:
		void loop();
};

int main()
{
	ArrayTime t1;
	clock_t t2,t3;
	t2 = clock(); 
	t1.loop();
	t3 = clock();

	float diff = ((float)t3 - (float)t2)/10000.0F;
	cout << "The run time is: " << diff << "\n";
	cin.ignore();
}

void ArrayTime::loop()
{
	double number1 = 100;
	double number2 = 10099;	
	double array1[10000];
	double array2[10000];

	for(int i = 0; i <10000; i++)
	{
		array1[i]= number1;
		array2[i] = number2;

		const double result = array1[i]* array2[i];
		
		number1++;
		number2--;
	}

	int i(1000000000);
        while(i--);
}
If all you want is to get some arbitrary number to print, maybe you should check out
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
Topic archived. No new replies allowed.