Time a function: Difference between revisions
Content added Content deleted
m (→{{header|Python}}: Wasn't that silly?) |
(added c++) |
||
Line 42:
Identity(4) takes 0.000001117 seconds.
Sum(4) takes: 0.000003632 seconds.
=={{header|C++}}==
#include <ctime>
#include <iostream>
using namespace std;
int identity(int x) { return x; }
int sum(int num) {
for (int i = 0; i < 1000000; i++)
num += i;
return num;
}
double time_it(int (*action)(int), int arg) {
clock_t start_time = clock();
action(arg);
clock_t finis_time = clock();
return ((double) (finis_time - start_time)) / CLOCKS_PER_SEC;
}
int main() {
cout << "Identity(4) takes " << time_it(identity, 4) << " seconds." << endl;
cout << "Sum(4) takes " << time_it(sum, 4) << " seconds." << endl;
return 0;
}
===Example===
Identity(4) takes 0 seconds.
Sum(4) takes 0.01 seconds.
=={{header|J}}==
|