cours_progra/bac2/os/chap3/ex4.c

30 lines
675 B
C
Raw Normal View History

2023-11-08 09:42:46 +01:00
#include <sys/time.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#define printvi(X) printf("%s = %d\n", #X, X);
#define printvf(X) printf("%s = %lld\n", #X, X);
typedef struct timespec timespec_t;
timespec_t start, end;
long long int dtime(timespec_t *start, timespec_t *end){
return (end->tv_sec - start->tv_sec) * 1e9 + end->tv_nsec - start->tv_nsec;
}
int main(void)
{
FILE* fd = fopen("plot", "w");
long long int time, time_sum;
for (int i = 0; i < 10000; ++i) {
clock_gettime(CLOCK_REALTIME, &start);
usleep(1);
clock_gettime(CLOCK_REALTIME, &end);
time = dtime(&start, &end);
time_sum += time;
fprintf(fd, "%d %lld\n", i, time);
}
}