Getrusage vs. clock_gettime ()

I am trying to get the CPU time spent on a process on Ubuntu. As far as I know, two functions can do this job: getrusage () and clock_gettime (CLOCK_PROCESS_CPUTIME_ID, & tp). In my code, calling getrusage () immediately after clock_gettime (CLOCK_PROCESS_CPUTIME_ID, & tp) always gives different results.

Can someone help me understand which function gives higher resolution and what advantages / disadvantages of these functions have?

Thank.

+5
source share
1 answer

getrusage (...)

  • Divide processor time into system and user components in ru_utime and ru_stime, respectively.
  • : struct timeval tv_usec, 4 /250 ()
  • SVr4, 4.3BSD, POSIX.1-2001: , Linux, OS X

.


clock_gettime (CLOCK_PROCESS_CPUTIME_ID,...)

  • /.
  • : struct timespec - struct timeval, tv_nsec tv_usec. , , clock_getres.
  • librt
  • . clock_gettime -1 errno EINVAL, getrusage. ()
  • SUSv2 POSIX.1-2001: , Linux, OS X.

.

0

All Articles