C语言跨平台时间操作计算时间差

头文件

代码语言:javascript
复制
#pragma once

#if defined(_WIN32)
#include<sys/timeb.h>
#if defined(UNIX)||defined(APPLE)
#include<time.h>
#endif

typedef long long long_time_t;

long_time_t TimeInMillisecond(void) {
#if defined(_WIN32)
struct timeb time_buffer;
ftime(&time_buffer);
return time_buffer.time1000LL+time_buffer.millitm;
#elif defined(UNIX)||defined(APPLE)
struct timeval time_value;
gettimeofday(&time_buffer,NULL);
return time_buffer.tv_sec
1000LL+time_buffer.tv_usec/1000;
#elif defined(STDC)&&STDC__VERSION==201112L
struct timespec timespec_value;
timespec_get(&timespec_value,TIME_UTC);
return timespec_value.tv_sec1000LL+timespec_value.tv_nsec/1000;
#else
time_t current_time = time(NULL);
return current_time
1000LL;
#endif
}
#endif

计算时间差

代码语言:javascript
复制
#include<stdio.h>
#include<time.h>
#include"../include/io_utils.h"
#include"../include/time_utils.h"
int main()
{
clock_t start_time = clock();
for(int i = 0;i<100;++i)
printf("hello world!");
clock_t end_time = clock();
PRINT_DOUBLE((end_time-start_time)*1.0/CLOCKS_PER_SEC);

return 0; 

}