C语言如何获得精确时间

C语言如何获得精确时间

精确到秒

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <time.h>

int main(){
time_t t_start, t_end;
t_start = time(NULL) ;
sleep(3000);
t_end = time(NULL) ;
printf("time: %.0f s\n", difftime(t_end,t_start)) ;
return 0;
}

精确到微秒

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <sys/timeb.h>

long long getSystemTime() {
struct timeb t;
ftime(&t);
return 1000 * t.time + t.millitm;
}

int main() {
long long start=getSystemTime();
sleep(3);
long long end=getSystemTime();

printf("time: %lld ms\n", end-start);
return 0;
}