#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
void timevalfunction(){
unsigned i = 0;
struct timeval start, end;
gettimeofday(&start, NULL);
for(i = 0;i < UINT32_MAX; i++);
gettimeofday(&end, NULL);
unsigned long duration = (end.tv_sec - start.tv_sec) * 1000000 + end.tv_usec - start.tv_usec;
printf("%luμs\n",duration);
}
void clockfunction(){
unsigned i = 0;
clock_t start, finish;
start = clock();
for(i = 0;i < UINT32_MAX; i++);
finish = clock();
unsigned long duration = finish - start;
printf( "%luμs\n", duration);
}
void timespecfunction(){
unsigned i = 0;
struct timespec start, end;
clock_gettime(CLOCK_REALTIME, &start);
for(i = 0;i < UINT32_MAX; i++);
clock_gettime(CLOCK_REALTIME, &end);
unsigned long duration = (end.tv_sec - start.tv_sec) * 1000000000 + end.tv_nsec-start.tv_nsec;
printf("%luns\n", duration);
}
int main(int argc, const char * argv[]) {
timevalfunction();
clockfunction();
timespecfunction();
return 0;
}
关于C语言时间间隔的,有需要的可以拿去