-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgettimeoftheday.c
More file actions
33 lines (25 loc) · 856 Bytes
/
gettimeoftheday.c
File metadata and controls
33 lines (25 loc) · 856 Bytes
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
28
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
int main(void){
struct timeval start, end;
double time_elapsed;
/* Rispetto all'esempio con time() il risultato sara' sicuramente molto
piu' accurato, poiche' esresso in millisecondi */
if (gettimeofday(&start, NULL) < 0) {
fprintf(stderr, "Err.(%s) getting time, start\n", strerror(errno));
exit(EXIT_FAILURE);
}
sleep(3);
if (gettimeofday(&end, NULL) < 0) {
fprintf(stderr, "Err.(%s) getting time, end\n", strerror(errno));
exit(EXIT_FAILURE);
}
time_elapsed = ((((end.tv_sec - start.tv_sec) * 1000000.) +
((end.tv_usec - start.tv_usec))) / 1000000.);
printf("Tempo trascorso: %f secondi\n", time_elapsed);
return(EXIT_SUCCESS);
}