关于使用clock()来测量任务的执行时间差的疑问
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include<pthread.h>
#include<unistd.h>
#include<errno.h>
#define TIMEGAP 1
void *fun_1()
{
printf("tid = %d\n",pthread_self());
clock_t start,end2;
start=clock(); //线程开始
double duration ;
while(1)
{
duration=(double)(end2-start)/CLOCKS_PER_SEC;
if(duration>1)
{
printf("time arrived!\n");
break;
}
end2=clock(); //结束时间
printf("end2=%d \n",end2);
usleep(1000);
}
}
int main()
{
clock_t start,end1;
double duration;
long i = 10000000L;
pthread_t tid;
int ret=pthread_create(&tid,NULL,fun_1,NULL);
if(!ret)
perror("pthread_create");
start=clock(); //主线程中测量执行时间,
while(i--);
end1=clock();
printf("end1=%d \n",end1);
duration=(double)(end1-start)/CLOCKS_PER_SEC;
printf("%f ",duration);
return 0;
}