I want to calculate the execution time of a pretty small function to compare the execution time of a recursive function vs iterative.
Surely, clock()
simply cannot do that with not enough resolution. Could you show me how can I use another time sources like GetThreadTimes()
. I saw a description on the Microsoft website, but did not catch the principle.
Also, <chrono>
header does not work in MS Visual 10.
The code:
int search (int a[], int size, int& num) {
if (size >0) {
if (a[size-1] == 17) {num = size-1; return num;}
else {return search (a, --size, num);}}
else {return num=-1;};
}
int searchit (int a[], int size, int& num) {
for (int i =0; i< size; i++) {
if (a[i] == 17) {num = i;}
else num = -1;
}
return num;}
int main () {
srand ((unsigned int) time(0));
int num = 0;
const int size = 40;
int a[size];
for (int i =0; i< size; i++) {
a[i] = rand()%100;
cout << a[i] << endl;}
cout << '\n';
search (a, size, num);
cout << num << endl;
cin.get();
cin.ignore();
}