I want to create a function that measures execution time of any function that is passed as it's argument no matter how many arguments the function that is passed has.
#include<stdio.h>
#include<time.h>
typedef void (*FUNC_PTR)(int, int);
void print_range(int n1, int n2)
{
int i;
for(i=n1; i<n2; i++){
printf("%d\n", i);
}
}
void measureTime(FUNC_PTR ptr, int n1, int n2)
{
time_t start_time = clock();
ptr(n1, n2);
time_t end_time = clock();
printf("%f\n", (double)end_time - start_time);
}
main()
{
int n1 = 1, n2 = 1000;
FUNC_PTR ptr = print_range;
measureTime(ptr, n1, n2);
}
I got it working for this specific case of passing print_range
that has 2 arguments.
Is there any way to make measureTime
function execute any function that is passed as FUNC_PTR ptr
without having to pass print_range
n1
and n2
arguments to measureTime
function.
So for instance function that is passed as ptr
can have any number of arguments and measureTime
will still work.
void measureTime(FUNC_PTR ptr)
{
time_t start_time = clock();
ptr;
time_t end_time = clock();
printf("%f\n", (double)end_time - start_time);
}
And if above can work how would than main look like?