Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting getrusage() to measure system time in C

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...

#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.

like image 457
Stout Joe Avatar asked May 09 '12 04:05

Stout Joe


People also ask

What does Getrusage do in C?

The getrusage() function shall provide measures of the resources used by the current process or its terminated and waited-for child processes. If the value of the who argument is RUSAGE_SELF, information shall be returned about resources used by the current process.

What does Getrusage return?

getrusage() returns resource usage measures for who, which can be one of the following: RUSAGE_SELF Return resource usage statistics for the calling process, which is the sum of resources used by all threads in the process.


2 Answers

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.

like image 170
Eric Melski Avatar answered Sep 22 '22 12:09

Eric Melski


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

like image 33
shinkou Avatar answered Sep 19 '22 12:09

shinkou