参考代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(int argc,char **argv){
struct timespec time_start = {0, 0}, time_end = {0, 0};
clock_gettime(CLOCK_REALTIME, &time_start); // 获取起始位置值
for (int i = 0; i < 1000; i++)
for (int j = 0; j < 1000; j++){
}
clock_gettime(CLOCK_REALTIME, &time_end); // 获取结束位置
printf("time:%llu s %llu ns", time_end.tv_sec-time_start.tv_sec, time_end.tv_nsec-time_start.tv_nsec);
// 打印换算出 s 和 ns 的值
return 0;
}