¿Cómo medir el tiempo de ejecución REAL de un programa C en Linux?

I know this question may have been commonly asked before, but it seems most of those questions are regarding the transcurrido time (based on wall clock) of a piece of code. The transcurrido time of a piece of code is unlikely equal to the real execution time, as other processes may be executing during the transcurrido 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()?

preguntado el 27 de agosto de 11 a las 16:08

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

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

Would clock_gettime satisfy your needs? They provide several clocks for most requirements. CLOCK_PROCESS_CPUTIME_ID might be what you need. -

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

3 Respuestas

You can check the real CPU time of a process on linux by utilizing the Tiempo de CPU 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;

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

Respondido 27 ago 11, 22:08

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

@Dillon Geo: No. It is CPU time consumed by the calling process between the two calls. - caf

@caf is right - to quote the Main CPU-Time-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

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

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

Respondido 27 ago 11, 22:08

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

Respondido 14 Jul 16, 14:07

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

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.