22

I know this question may have been commonly asked before, but it seems most of those questions are regarding the elapsed time (based on wall clock) of a piece of code. The elapsed time of a piece of code is unlikely equal to the actual execution time, as other processes may be executing during the elapsed time of the code of interest.

I used getrusage() to get the user time and system time of a process, and then calculate the actual execution time by (user time + system time). I am running my program on Ubuntu. Here are my questions:

  1. How do I know the precision of getrusage()?
  2. Are there other approaches that can provide higher precision than getrusage()?
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Dillon Geo
  • 271
  • 1
  • 2
  • 6
  • In a normal operating system, if there are no other user programs running, the difference between the actual execution time and elapsed time should not be important. Usually the actual execution time is directly proportional with the elapsed time. – Andrei Bozantan Aug 27 '11 at 16:32
  • 1
    @Andrei: You're assuming that the program doesn't do any I/O operations (that wait for the disk or the network) and is entirely CPU bound. This only holds for few programs. – Codo Aug 27 '11 at 16:55
  • 1
    Define “actual execution time”. If it’s not wall clock and it’s not the user+system time, then what is it? When you want more precision, what do you mean, nanosecond not microsecond? Beware that nanosecond precision is probably fictional, as [most Unix kernels don’t provide that sort of thing](http://www.eecis.udel.edu/~mills/precise.html). – tchrist Aug 27 '11 at 17:29
  • 1
    Would clock_gettime satisfy your needs? They provide several clocks for most requirements. CLOCK_PROCESS_CPUTIME_ID might be what you need. –  Aug 27 '11 at 17:37
  • 2
    In a multi-threaded program on a multi-CPU machine, the CPU time for a process could be much higher than the elapsed time for the process (up to a factor of N higher, where N is the number of separate processors). – Jonathan Leffler Aug 27 '11 at 19:30

3 Answers3

19

You can check the real CPU time of a process on linux by utilizing the CPU Time functionality of the kernel:

 #include <time.h>

 clock_t start, end;
 double cpu_time_used;

 start = clock();
 ... /* Do the work. */
 end = clock();
 cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

Source: http://www.gnu.org/s/hello/manual/libc/CPU-Time.html#CPU-Time

That way, you count the CPU ticks or the real amount of instructions worked upon by the CPU on the process, thus getting the real amount of work time.

Lars
  • 5,757
  • 4
  • 25
  • 55
  • It seems this method measures the elapsed between the two calls to clock(), that is, the measured time may include the time spent on other processes, am I correct? – Dillon Geo Aug 28 '11 at 03:41
  • 3
    @Dillon Geo: No. It is CPU time consumed by the calling process between the two calls. – caf Aug 29 '11 at 05:31
  • 1
    @caf is right - to quote the [Main CPU-Time](http://www.linuxselfhelp.com/gnu/glibc/html_chapter/libc_21.html)-Page: _CPU time is like calendar time, except that it is based on the subset of the time continuum when a particular process is actively using a CPU. **CPU time is, therefore, relative to a process**._ If my answer helped and was correct, could you please upvote / accept as solution? Thanks! :) – Lars Aug 29 '11 at 06:37
  • On 64-bit machines, `clock()` may be the best solution as it can be implemented on top of the best system call available, though this depends on the implementation. According to the Linux clock(3) man page: "In glibc 2.17 and earlier, clock() was implemented on top of times(2). For improved precision, since glibc 2.18, it is implemented on top of clock_gettime(2) (using the CLOCK_PROCESS_CPUTIME_ID clock)." – vinc17 Sep 11 '14 at 14:37
5

The getrusage() function is the only standard/portable way that I know of to get "consumed CPU time".

There isn't a simple way to determine the precision of returned values. I'd be tempted to call the getrusage() once to get an initial value, and the call it repeatedly until the value/s returned are different from the initial value, and then assume the effective precision is the difference between the initial and final values. This is a hack (it would be possible for precision to be higher than this method determines, and the result should probably be considered a worst case estimate) but it's better than nothing.

I'd also be concerned about the accuracy of the values returned. Under some kernels I'd expect that a counter is incremented for whatever code happens to be running when a timer IRQ occurs; and therefore it's possible for a process to be very lucky (and continually block just before the timer IRQ occurs) or very unlucky (and unblock just before the timer IRQ occurs). In this case "lucky" could mean a CPU hog looks like it uses no CPU time, and "unlucky" could means a process that uses very little CPU time looks like a CPU hog.

For specific versions of specific kernels on specific architecture/s (potentially depending on if/when the kernel is compiled with specific configuration options in some cases), there may be higher precision alternatives that aren't portable and aren't standard...

Brendan
  • 35,656
  • 2
  • 39
  • 66
0

You can use this piece of code :

#include <sys/time.h>
struct timeval start, end;
gettimeofday(&start, NULL);
.
.
.
gettimeofday(&end, NULL);
delta = ((end.tv_sec  - start.tv_sec) * 1000000u +
         end.tv_usec - start.tv_usec) / 1.e6;
printf("Time is : %f\n",delta);

It will show you the execution time for piece of your code

pooria
  • 343
  • 2
  • 14
  • 1
    Downvoted because this is measuring wall clock time using `gettimeofday()`. OP asked for CPU time (i.e. when the process is not executing, the elapsed time counter should stop.) – Matthew Cole Apr 07 '17 at 19:51