1

I need to measure time of some sorting algorithms. The problem is that some funcions(algorithms) might have 2 arguments(bubble sort for example) or 3 arguments(quick sort). I don't have idea how to prepare my function to accept both type of functions(2-arguments and 3-arguments).

I was thinking about add some default argument to every function which have only 2 arguments and then in my get_time_calculations declares it like this:

double get_time_calculations(void (*f)(int *, int, int), int *tab, int n) {
    ...
}

But this solution is only workaround, and maybe there is a better way to did this without changing arguments of prepared algorithms?

My function to measure execution of function time:

double get_time_calculations(void (*f)(int *, int), int *tab, int n) {
    clock_t begin, end;
    begin = clock();
    f(tab, n);
    end = clock();
    randomize(tab, n, -100, 201);
    return (double)(end - begin)/CLOCKS_PER_SEC;
}

And my function to test all algorithms:

void test1(int *ran_tab, int n) {

    cout << endl << "Insert sort: " << get_time_calculations(insert_sort, ran_tab, n) << "ms" << endl;
    cout << endl << "Bubble sort: " << get_time_calculations(bubble_sort, ran_tab, n) << "ms" << endl;
    cout << endl << "Selection sort: " << get_time_calculations(selection_sort, ran_tab, n) << "ms" << endl;
    cout << endl << "Quick sort: " << get_time_calculations(quick_sort, ran_tab, n) << "ms" << endl;
    cout << endl << "Shell sort: " << get_time_calculations(shell_sort, ran_tab, n) << "ms" << endl;
    cout<<endl<<"Heap sort: "<<get_time_calculations(heap_sort, ran_tab, n)<<"ms"<<endl;
}
Eddy
  • 593
  • 8
  • 22
  • Off-topic: Why are you randomizing **inside** `get_time_calculations`? – Holt May 11 '19 at 12:18
  • 1
    @πάντα-ῥεῖ I don't think this is a good duplicate, while `std::function`-like structure are required in the other question, here other options could be used (e.g., templated `get_time_calculations` + lambda), but such answers would not fit the duplicate. – Holt May 11 '19 at 12:22
  • @Holt If you have better ones at hand, I am willing to add these to the list. Anyways `std::function` and perfect forwarding is the usual technique to solve that kind of problems with c++-7 – πάντα ῥεῖ May 11 '19 at 12:24
  • Not the fanciest solution but in C++ you can define a function multiple times with the same name with different arguments. – Croppi May 11 '19 at 12:25
  • @πάνταῥεῖ (I re-opened this question by mistake, sorry). I do not have a better one at hand, but I don't think the one you put is sufficient, I'll for a closer one or let you know if I don't find one. – Holt May 11 '19 at 12:25

0 Answers0