Recursive timer implementation

I have a quick question on how I can implement a timer where it will keep track of and display how long it took to execute the program.

For context my program will ask you to input a size of an array less than 10 and enter in some numbers. After that it will swap numbers to make it in numerical order. Not sure if you needed that info, but there you go. I tried to do this, but to no avail. Any help will be appreciated!
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
73
74
75
76
77
78
79
80
81
  #include <iostream>
#include <ctime>
using namespace std;

void swap(int& val1, int& val2);
int partition(int arr[], int start, int end);
void quickSort(int arr[], int start, int end);
int counter = 0;

void swap(int& val1, int& val2)
{
	int temp = val1;//putting value 1 in a temp container
	val1 = val2;
	val2 = temp;
}

int partition(int arr[], int start, int end)
{
	int pivot = arr[end];//choosing our pivot at the end of the array of numbers (you can choose the pivot in any postion)
	int i = (start - 1);//do this to continue down the path of arrays
	for (int j = start; j <= end - 1; j++)//j begins at the start of the arry and when. Now we compare the j to the pivot and move on 
	{
		counter++;//counting comparisons
		if (arr[j] <= pivot)//current value being scanned is smaller than or equal to pivot
		{
			i++;//increment index
			swap(arr[i], arr[j]);//swap the two values of i and j
		}
	}
	swap(arr[i + 1], arr[end]);//swaping the pivot now infront of where the i is hence the +1
	return(i + 1);//returning the info
}

void quickSort(int arr[], int start, int end)
{
	if (start < end)//if the starting point is less than the pivot
	{
		int pi = partition(arr, start, end);
		quickSort(arr, start, pi - 1);
		quickSort(arr, pi + 1, end);
	}
}
int const MaxElements = 10;
int main()
{
	clock_t before;
	clock_t after;
	double result;
	int* arr;
	int max[MaxElements];
	int size = 0, start = 0, end = size - 1;
	cout << "Enter size of set (At most 10 seperate inputs): ";
	cin >> size;
	arr = new int[size];
	if (size >= MaxElements)
	{
		cout << "set size must be less than " << MaxElements << "\n";
		system("PAUSE");
		exit(1);
	}
	for (int i = 0; i < size; i++)
	{
		cout << "Input value:";
		cin >> arr[i];
	}
	cout << "Numbers Entered: ";
	for (int i = 0; i < size; i++)
	{	
		cout << arr[i] << " ";
	}
	end = size - 1;
	quickSort(arr, start, end);
	cout << endl << "Numbers Sorted: " << endl;
	for (int i = 0; i < size; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl << "Comparisons: " << counter;
	
}
Something like this, perhaps:

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
#include <iostream>
#include <ctime>
#include <type_traits>
#include <chrono>
#include <algorithm>

struct timer
{
    // use the high resolution clock if it is monotonic; steady clock otherwise
    using wall_clock = std::conditional< std::chrono::high_resolution_clock::is_steady,
                                         std::chrono::high_resolution_clock,
                                         std::chrono::steady_clock >::type ;

    // get the current time points on construction
    const wall_clock::time_point wall_clock_start = wall_clock::now() ;
    const std::clock_t cpu_clock_start = std::clock() ;

    ~timer() // print elapsed times on destruction
    {
        // get current time points on destruction
        const auto cpu_clock_end = std::clock() ;
        const auto wall_clock_end = wall_clock::now() ;

        // calculate elapsed time in milliseconds
        const auto cpu_elapsed = (cpu_clock_end-cpu_clock_start) * 1000.0 / CLOCKS_PER_SEC ;
        using namespace std::chrono ;
        const auto wall_clock_elapsed = duration_cast<milliseconds>(wall_clock_end-wall_clock_start).count() ;

        // print results
        std::cout << " elapsed processor time: " << cpu_elapsed << " milliseconds\n"
                  << "elapsed wall clock time: " << wall_clock_elapsed << " milliseconds\n" ;
    }
};

int main()
{
    {
        timer t ; // instantiate a timer

        // put the code to be timed here. for example:
        std::cout << "enter an integer: " ;
        int i ;
        std::cin >> i ;
        volatile int j = i ;
        for( i = 0 ; i < 100'000'000 ; ++i ) j = -j ;

        // elapsed times would be printed out when t is destroyed at the end of the scope
    }
}


The size of the sequence being sorted needs to be much larger than ten (say a few million) to get measurable elapsed (processor) times.
Last edited on
Topic archived. No new replies allowed.