1

所以我使用 rusage 打印出用户需要多长时间,以及系统处理命令需要多长时间,类似于,

           //DO STUFF HERE
           printf(" TOTAL TIMES: ");
           tusage.ru_utime.tv_sec  = rusage.ru_utime.tv_sec + rusage.ru_stime.tv_sec;
4

1 回答 1

1

尝试这个:

#include <stdio.h>
#include <sys/resource.h>
int main() {
  struct rusage rusage;
  struct rusage tusage;
  int i, j, r=0;
  for (i = 0; i < 10000; i++) {
    for (j = 1; j < 100000; j++) {
      r = i % j + i / j;
    }
  }
  getrusage(RUSAGE_SELF, &rusage);
  printf("TOTAL TIME \n");
  tusage.ru_utime.tv_sec =  rusage.ru_utime.tv_sec + rusage.ru_stime.tv_sec;
  tusage.ru_utime.tv_usec = rusage.ru_utime.tv_usec + rusage.ru_stime.tv_usec;
  tusage.ru_utime.tv_sec += tusage.ru_utime.tv_usec / 1000000;
  tusage.ru_utime.tv_usec = tusage.ru_utime.tv_usec % 1000000;
  printf("%ld.%06ld\n", tusage.ru_utime.tv_sec, tusage.ru_utime.tv_usec);

  return r;
}
于 2013-03-14T08:15:23.860 回答