Chinaunix首页 | 论坛 | 博客
  • 博客访问: 542082
  • 博文数量: 104
  • 博客积分: 4131
  • 博客等级: 上校
  • 技术积分: 1137
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-31 15:05
文章分类

全部博文(104)

文章存档

2011年(13)

2010年(23)

2009年(68)

我的朋友

分类: LINUX

2009-08-26 16:34:43

linux下计时函数的使用目的是测量程序运行的时间
头文件
#include
计时函数
int gettimeofday(struct timeval *tv,struct timezone *tz);
保存时间的结构体
strut timeval {
long tv_sec; /* 秒数 */
long tv_usec; /* 微秒数 */
};
以下是自己写的测试程序:
#include
#include
int main()
{
struct timeval tpstart,tpend;
 double timeuse;
 int i=0;
 gettimeofday(&tpstart,NULL);
 for(i=0;i<1000000000;i++);
 //printf("%d ",i);
 gettimeofday(&tpend,NULL);
 timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
 timeuse/=1000000;
 printf("processor time is %lf s\n",timeuse);
 return 0;
}
这段程序在我的虚拟机下测试的时间是3.595490s。
阅读(2187) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~