1:clock函数
函数原型: clock_t clock(void)
demo:
#include<stdio.h>
#include<time.h>
#include <unistd.h>
int main()
{
clock_t start_t,finish_t;
double total_t = 0;
int i = 0;
start_t = clock();
for(;i<100000;++i)
{
usleep(1)
}
finish_t = clock();
total_t = (double)(finish_t - start_t) / CLOCKS_PER_SEC;//将时间转换为秒
printf("cost %f\n", total_t);
return 0;
}
使用注意项:
多线程时会很不准,多线程不建议使用,并且clock之间使用sleep并不会计时
2:time函数
函数原型: time_t time(time_t *timer)
描述从1970-01-01 00:00:00到现在的秒数
demo:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main ()
{
time_t seconds;
while(1)
{
seconds = time(NULL);
printf("time %ld\n", seconds);
sleep(1);
}
return(0);
}
使用注意项:
系统时间改变时会变,如果作为时间差统计慎用