1.不使用sleep,循环10000次
/* 1.c */
#include
#include
void main(void)
{
struct timeval start, end;
int i;
float time_use=0;
gettimeofday(&start, NULL);
for(i=0; i<10000; i++)
{
;
}
gettimeofday(&end, NULL);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %.10f\n",time_use);
}
运行结果:
time_use is 51.0000000000
2.使用sleep(0),循环10000次
/* 0.c */
#include
#include
void main(void)
{
struct timeval start, end;
int i;
float time_use=0;
gettimeofday(&start, NULL);
for(i=0; i<10000; i++)
{
sleep(0);
}
gettimeofday(&end, NULL);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %.10f\n",time_use);
}
运行结果:
time_use is 83.0000000000
3.使用usleep(0),循环10000次
/* 4.c */
#include
#include
void main(void)
{
struct timeval start, end;
int i;
float time_use=0;
gettimeofday(&start, NULL);
for(i=0; i<10000; i++)
{
usleep(0);
}
gettimeofday(&end, NULL);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %.10f\n",time_use);
}
运行结果:
time_use is 541044.0000000000
4.使用usleep(1),循环10000次
/* 2.c */
#include
#include
void main(void)
{
struct timeval start, end;
int i;
float time_use=0;
gettimeofday(&start, NULL);
for(i=0; i<10000; i++)
{
usleep(1);
}
gettimeofday(&end, NULL);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %.10f\n",time_use);
}
运行结果:
time_use is 553434.0000000000
5.使用usleep(2),循环10000次
/* 9.c */
#include
#include
void main(void)
{
struct timeval start, end;
int i;
float time_use=0;
gettimeofday(&start, NULL);
for(i=0; i<10000; i++)
{
usleep(2);
}
gettimeofday(&end, NULL);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %.10f\n",time_use);
}
运行结果:
time_use is 560403.0000000000
6.使用nanosleep,休眠0纳秒,循环10000次
/* 10.c */
#include
#include
void main(void)
{
struct timeval start, end;
int i;
float time_use=0;
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 0;
gettimeofday(&start, NULL);
for(i=0; i<10000; i++)
{
nanosleep(&ts,NULL);
}
gettimeofday(&end, NULL);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %.10f\n",time_use);
}
运行结果:
time_use is 542908.0000000000
7.使用nanosleep,休眠1纳秒,循环10000次
/* 3.c */
#include
#include
void main(void)
{
struct timeval start, end;
int i;
float time_use=0;
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 1;
gettimeofday(&start, NULL);
for(i=0; i<10000; i++)
{
nanosleep(&ts,NULL);
}
gettimeofday(&end, NULL);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %.10f\n",time_use);
}
运行结果:
time_use is 548566.0000000000
8.使用nanosleep,休眠1000纳秒,循环10000次
/* 7.c */
#include
#include
void main(void)
{
struct timeval start, end;
int i;
float time_use=0;
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 1000;
gettimeofday(&start, NULL);
for(i=0; i<10000; i++)
{
nanosleep(&ts,NULL);
}
gettimeofday(&end, NULL);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %.10f\n",time_use);
}
运行结果:
time_use is 551212.0000000000
9.使用nanosleep,休眠10000纳秒,循环10000次
/* 8.c */
#include
#include
void main(void)
{
struct timeval start, end;
int i;
float time_use=0;
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 10000;
gettimeofday(&start, NULL);
for(i=0; i<10000; i++)
{
nanosleep(&ts,NULL);
}
gettimeofday(&end, NULL);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %.10f\n",time_use);
}
运行结果:
time_use is 644741.0000000000
10.使用usleep(10),循环10000次
/* 5.c */
#include
#include
void main(void)
{
struct timeval start, end;
int i;
float time_use=0;
gettimeofday(&start, NULL);
for(i=0; i<10000; i++)
{
usleep(10);
}
gettimeofday(&end, NULL);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %.10f\n",time_use);
}
运行结果:
time_use is 641488.0000000000
11.每循环1000次,使用1次usleep(1),共循环10000次
/* 11.c */
#include
#include
void main(void)
{
struct timeval start, end;
int i,j;
float time_use=0;
gettimeofday(&start, NULL);
for(i=0,j=0; i<10000; i++,j++)
{
if(0 == j%1000)
{
usleep(1);
}
}
gettimeofday(&end, NULL);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %.10f\n",time_use);
}
运行结果:
time_use is 634.0000000000