C++: I can't properly get the processing time for this program. Please help. clock_t?

This is a code which sorts arrays.

I use clock_t to determine the processing time for my code. However, it always stacks with the time taken from before. It looks like it's just adding the 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
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
int main(int argc, char *argv[])
{unsigned long n;//size of array -- long for big numbers of n
int choice, choice2, sort;//numbers corresponding to the choices(switch)
clock_t Begin, End;
float elapTicks, elapMilli;
wrongchoice://goto function -- bringing back to this point after entering wrong choice
cout<<"===============================…
cout<<"\t\t\tChoose the order of the output:\n\t\t\t\t[1] Random-Descending\n\t\t\t\t[2] Ascending-Descending\n\t\t\t\t[3] Descending-Ascending\n\t\t\t\t[4] Random-Ascending\n\t\t\tChoice: ";
cin>>choice;
cout<<"\t\t\tIndicate size of array: ";
cin>>n;
long int array[n];//first and original array to be used all through the program
long int arrayb[n];//second/temporary array used to preserve/copy original values of first array
for (int i=0; i<n; i++)
{array[i]=rand()%99999;
arrayb[i]=array[i];//copy value of the first array into the second array
}
double temptime=0;
clock_t realend, realend2, realend3, realend4, realend5, realend6;
sorting:
cout<<"\n\t\t\t\t[1] Bubble Sort\n\t\t\t\t[2] Selection Sort\n\t\t\t\t[3] Insertion Sort\n\t\t\t\t[4] Quick Sort\n\t\t\t\t[5] Shell Sort\n\t\t\t\t[6] Merge Sort\n";
cout<<"\t\t\tChoose what sorting algorithm to use: ";
cin>>sort;
goto copyarray;//goto function to copy array elements(located at line 747)
sorting2://goto function -- used to resume back to this point after copying array elements
srand(time(NULL));
switch(choice)//first switch -- indicates the desired order of the output
{
case 1://Random-Descending
{cout<<"==============================…
cout<<"\t\t\tUnsorted Array: "<<endl;
for(int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\n\t\t\tSorted Array:\n";
switch(sort)//second swtich -- indicates what sorting algorithm to use
{
case 1:
{//Bubble Sort Descending
clock_t Begin1, End1;
realend=clock();
Begin1=clock();//indicates start of timer/clock counter(every start of cases for sorting)
BubbleSort2(array, n);//calling of function
End1=clock();//indicates end of timer/clock
float time1;
time1= (End1-Begin1/1000.00f);//used to convert value(taken from the clock code) to milliseconds(from seconds)
cout<<"Bubble Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<time1<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 2:
{//Selection Sort Descending
clock_t Begin2, End2;
realend2=clock();
Begin2=clock();
double temptime2=Begin2;
SelectionSort2(array, n);//calling of function
End2=clock();
float time2;
time2 = (End2-Begin2/1000.00f);
cout<<"Selection Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<time2<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}


That is some part of my code. I couldn't put it all here since it's too long. The time adds up with the time that is first taken when I choose another option from the switch code, even though I declared a new variable for clock_t. Please help T_T I really don't know what's wrong with the code.

Here's the entire code:

http://www.daniweb.com/software-development/cpp/threads/416130/1775548#post1775548



Feel free to paste it in any c++ debugger so you would understand the code better.

I hope someone can help me.
It would be easier to understand if you posted stuff that:

was indented.

used whitespace.

compiled.

Wasn't sprinkled with goto.

Was whittled down to a (compilable) example of reasonable size that demonstrated the problem.
Look at this http://www.cplusplus.com/reference/clibrary/ctime/clock/ to use clock() properly

EDIT: This (End1-Begin1/1000.00f) doesn't convert seconds to miliseconds. Even if it did you won't get more accuracy from calculating seconds to milliseconds
Last edited on
Topic archived. No new replies allowed.