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
|
#include <iostream>
#include <ctime>
using namespace std;
void fun_cost(long (*func)(int), int n)
{
time_t begin, end;
time(&begin);
long result = (*func)(n);
time(&end);
double difference = difftime(end, begin);
const char *format = "n: %d result: %ld cost: %f";
int len = snprintf(NULL, 0, format, n, result, difference);
char *s = (char *)malloc(len + 1);
sprintf(s, format, n, result, difference);
std::cout << s << endl;
}
int main()
{
fun_cost(fab1, 45);
fun_cost(fab2, 45);
fun_cost(fab3, 45);
return 0;
}
|