获取getrusage()来测量C中的系统时间

时间:2021-10-04 21:29:53

I would like to measure the system time it takes to execute some code. To do this I know I would sandwich said code between two calls to getrusage(), but I get some unexpected results...

我想测量执行一些代码所需的系统时间。要做到这一点,我知道我会在两次调用getrusage()之间将所述代码夹在中间,但是我得到了一些意想不到的结果......

#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <stdio.h>

int main() {
  struct rusage usage;
  struct timeval start, end;
  int i, j, k = 0;

  getrusage(RUSAGE_SELF, &usage);
  start = usage.ru_stime;
  for (i = 0; i < 10000; i++) {
    /* Double loop for more interesting results. */
    for (j = 0; j < 10000; j++) {
      k += 20; 
    }
  }
  getrusage(RUSAGE_SELF, &usage);
  end = usage.ru_stime;

  printf("Started at: %ld.%lds\n", start.tv_sec, start.tv_usec);
  printf("Ended at: %ld.%lds\n", end.tv_sec, end.tv_usec);
  return 0;
}

I would hope that this produces two different numbers, but alas! After seeing my computer think for a second or two, this is the result:

我希望这会产生两个不同的数字,但唉!看到我的电脑想了一两秒后,这就是结果:

Started at: 0.1999s
Ended at: 0.1999s

Am I not using getrusage() right? Why shouldn't these two numbers be different? If I am fundamentally wrong, is there another way to use getrusage() to measure the system time of some source code? Thank you for reading.

我没有使用getrusage()吗?为什么这两个数字不应该不同?如果我从根本上说错了,有没有其他方法可以使用getrusage()来测量某些源代码的系统时间?谢谢你的阅读。

3 个解决方案

#1


10  

You're misunderstanding the difference between "user" and "system" time. Your example code is executing primarily in user-mode (ie, running your application code) while you are measuring, but "system" time is a measure of time spent executing in kernel-mode (ie, processing system calls).

你误解了“用户”和“系统”时间之间的区别。在测量时,您的示例代码主要在用户模式下执行(即运行应用程序代码),但“系统”时间是在内核模式(即处理系统调用)中执行所花费的时间的度量。

ru_stime is the correct field to measure system time. Your test application just happens not to accrue any such time between the two points you check.

ru_stime是测量系统时间的正确字段。您的测试应用程序恰好不会在您检查的两点之间产生任何这样的时间。

#2


2  

You should use usage.ru_utime, which is user CPU time used, instead.

您应该使用usage.ru_utime,即使用的用户CPU时间。

#3


0  

Use gprof. This will give give you the time taken by each function. Install gprof and use these flags for compilation -pg -fprofile-arcs -ftest-coverage.

使用gprof。这将给你每个功能所花费的时间。安装gprof并使用这些标志进行编译-pg -fprofile-arcs -ftest-coverage。

#1


10  

You're misunderstanding the difference between "user" and "system" time. Your example code is executing primarily in user-mode (ie, running your application code) while you are measuring, but "system" time is a measure of time spent executing in kernel-mode (ie, processing system calls).

你误解了“用户”和“系统”时间之间的区别。在测量时,您的示例代码主要在用户模式下执行(即运行应用程序代码),但“系统”时间是在内核模式(即处理系统调用)中执行所花费的时间的度量。

ru_stime is the correct field to measure system time. Your test application just happens not to accrue any such time between the two points you check.

ru_stime是测量系统时间的正确字段。您的测试应用程序恰好不会在您检查的两点之间产生任何这样的时间。

#2


2  

You should use usage.ru_utime, which is user CPU time used, instead.

您应该使用usage.ru_utime,即使用的用户CPU时间。

#3


0  

Use gprof. This will give give you the time taken by each function. Install gprof and use these flags for compilation -pg -fprofile-arcs -ftest-coverage.

使用gprof。这将给你每个功能所花费的时间。安装gprof并使用这些标志进行编译-pg -fprofile-arcs -ftest-coverage。