Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
39
Task/Time-a-function/C/time-a-function.c
Normal file
39
Task/Time-a-function/C/time-a-function.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int identity(int x) { return x; }
|
||||
|
||||
int sum(int s)
|
||||
{
|
||||
int i;
|
||||
for(i=0; i < 1000000; i++) s += i;
|
||||
return s;
|
||||
}
|
||||
|
||||
#ifdef CLOCK_PROCESS_CPUTIME_ID
|
||||
/* cpu time in the current process */
|
||||
#define CLOCKTYPE CLOCK_PROCESS_CPUTIME_ID
|
||||
#else
|
||||
/* this one should be appropriate to avoid errors on multiprocessors systems */
|
||||
#define CLOCKTYPE CLOCK_MONOTONIC
|
||||
#endif
|
||||
|
||||
double time_it(int (*action)(int), int arg)
|
||||
{
|
||||
struct timespec tsi, tsf;
|
||||
|
||||
clock_gettime(CLOCKTYPE, &tsi);
|
||||
action(arg);
|
||||
clock_gettime(CLOCKTYPE, &tsf);
|
||||
|
||||
double elaps_s = difftime(tsf.tv_sec, tsi.tv_sec);
|
||||
long elaps_ns = tsf.tv_nsec - tsi.tv_nsec;
|
||||
return elaps_s + ((double)elaps_ns) / 1.0e9;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("identity (4) takes %lf s\n", time_it(identity, 4));
|
||||
printf("sum (4) takes %lf s\n", time_it(sum, 4));
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue