#include <time.h>
double difftime( time_t time2, time_t time1 );
函数返回时间参数 time2 和 time1 之差的秒数表示。
#include <stdio.h>
#include <time.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
int main ()
{
time_t start_t, end_t;
double diff_t;
printf("程序启动...\n");
time(&start_t);
printf("休眠 5 秒...\n");
sleep(5);
time(&end_t);
diff_t = difftime(end_t, start_t);
printf("执行时间 = %f\n", diff_t);
printf("程序退出...\n");
return(0);
}输出结果:
程序启动... 休眠 5 秒... 执行时间 = 5.000000 程序退出...