我正在用Boost.unit编写一个单元测试,在单元测试的一部分中,我正在测试的代码不能超过单个CPU的50%。我如何在源代码中做出这个断言?

最佳答案

使用times呼叫-根据手册页:
姓名
时间-获取进程时间
简介
#包括

   clock_t times(struct tms *buf);

说明
times()将当前进程时间存储在buf指向的结构tms中。结构
tms定义如下:
       struct tms {
           clock_t tms_utime;  /* user time */
           clock_t tms_stime;  /* system time */
           clock_t tms_cutime; /* user time of children */
           clock_t tms_cstime; /* system time of children */
       };

   The tms_utime field contains the CPU  time  spent  executing  instructions  of  the  calling
   process.   The  tms_stime  field  contains  the CPU time spent in the system while executing
   tasks on behalf of the calling process.  The  tms_cutime  field  contains  the  sum  of  the
   tms_utime  and  tms_cutime  values  for  all waited-for terminated children.  The tms_cstime
   field contains the sum of the tms_stime and tms_cstime values for all waited-for  terminated
   children.

10-07 16:34
查看更多