您的位置:首页 > 运维架构 > Linux

测试时间的程序

2013-10-14 12:17 246 查看
#include<sys/time.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

/*算法*/
void function()
{
unsigned int i,j;
double y;
for(i=0;i<100;i++)
for(j=0;j<100;j++)
{usleep(10);y++;}
}

main()
{
struct timeval tpstart,tpend;
float timeuse;

gettimeofday(&tpstart,NULL);//获取开始运行时间
function();
gettimeofday(&tpend,NULL);//结束时间
/*计算算法执行时间*/
timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
timeuse/=1000000;
printf("Used Time:%f sec.\n",timeuse);
exit(0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C linux 时间