简体   繁体   English

如何在Linux下测量C程​​序的ACTUAL执行时间?

[英]How to measure the ACTUAL execution time of a C program under Linux?

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). 我使用getrusage()来获取进程的用户时间和系统时间,然后通过(用户时间+系统时间)计算实际执行时间。 I am running my program on Ubuntu. 我在Ubuntu上运行我的程序。 Here are my questions: 这是我的问题:

  1. How do I know the precision of getrusage()? 我怎么知道getrusage()的精确度?
  2. Are there other approaches that can provide higher precision than getrusage()? 还有其他方法可以提供比getrusage()更高的精度吗?

You can check the real CPU time of a process on linux by utilizing the CPU Time functionality of the kernel: 您可以通过利用内核的CPU Time功能来检查Linux上进程的实际CPU时间:

 #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 资料来源: 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. 这样,您可以计算CPU的滴答或CPU处理过程中的实际指令数量,从而获得实际的工作时间。

The getrusage() function is the only standard/portable way that I know of to get "consumed CPU time". getrusage()函数是我所知道的唯一标准/可移植方式,以获得“消耗的CPU时间”。

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. 我很想调用getrusage()一次得到一个初始值,并重复调用它直到返回的值与初始值不同,然后假设有效精度是初始值和最终值之间的差值值。 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; 在某些内核下,我希望当计时器IRQ出现时,计数器会在正在运行的任何代码中递增; 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). 因此,一个进程可能非常幸运(并且在计时器IRQ发生之前不断阻塞)或非常不幸(并且在计时器IRQ发生之前解锁)。 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. 在这种情况下,“幸运”可能意味着CPU占用看起来不使用CPU时间,而“不幸”可能意味着使用非常少的CPU时间的进程看起来像CPU占用。

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... 对于特定体系结构上的特定内核的特定版本(可能取决于在某些情况下是否/何时使用特定配置选项编译内核),可能存在更高精度的替代方案,这些方案不可移植且不是标准的......

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 它将显示您的代码片段的执行时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM