打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
times系统调用的例子
times系统调用可以得到进程所消耗的时间,man 2times可以得到times系统调用的帮助。
 

#include <sys/times.h>

clock_t times(struct tms *buf);

struct tms {
  clock_t tms_utime;  /* usertime */
  clock_t tms_stime;  /* systemtime */
  clock_t tms_cutime; /* user time of children*/
  clock_t tms_cstime; /* system time of children*/
};

 

其中时间都是以时钟滴答数(clocktick)为单位,并不能保证非常精确。在2.6内核,1秒钟是100次。


例子:

#include <sys/times.h>
#include <unistd.h>
#include <stdio.h>

int main() {
  struct tms buf;
  int i;
  clock_t c1, c2;
  FILE* fp;
  long CLK_TCK;

  c1 = times(&buf);
  // 这段代码用来消耗时间
  for(i=0;i<1000000;i++) {
    fp =fopen("/tmp/1.tmp", "rb+");
   fclose(fp);
  }
  c2 = times(&buf);
  if(c2 == -1) {
   perror("Failed");
  }

  printf("clock used = %d\n",c2 - c1);
  printf("tms_utime = %d\n", buf.tms_utime);
  printf("tms_stime = %d\n", buf.tms_stime);
  printf("tms_cutime = %d\n",buf.tms_cutime);
  printf("tms_cstime = %d\n",buf.tms_cstime);

  CLK_TCK =sysconf(_SC_CLK_TCK);
  printf("CLK_TCK = %d\n", CLK_TCK);
}

显示结果:

clock used = 354
tms_utime = 79
tms_stime = 274
tms_cutime = 0
tms_cstime = 0
CLK_TCK = 100

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
linux时间函数
186 f0608
单片机读写SD卡最简单最基本的程序
避免弯路:教你RT-Thread完美移植!
DSP程序开发与优化经验之二:程序运行时间测量方法总结
clock get ns
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服