Linux下如何测量C程序的ACTUAL执行时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7215764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to measure the ACTUAL execution time of a C program under Linux?
提问by Dillon Geo
I know this question may have been commonly asked before, but it seems most of those questions are regarding the elapsedtime (based on wall clock) of a piece of code. The elapsedtime of a piece of code is unlikely equal to the actualexecution time, as other processes may be executing during the elapsedtime 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:
我使用getrusage()获取一个进程的用户时间和系统时间,然后通过(用户时间+系统时间)计算实际执行时间。我在 Ubuntu 上运行我的程序。以下是我的问题:
- How do I know the precision of getrusage()?
- Are there other approaches that can provide higher precision than getrusage()?
- 我如何知道 getrusage() 的精度?
- 是否有其他方法可以提供比 getrusage() 更高的精度?
回答by Brendan
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. 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.
没有一种简单的方法可以确定返回值的精度。我很想调用 getrusage() 一次来获取初始值,然后重复调用它直到返回的值与初始值不同,然后假设有效精度是初始值和最终值之间的差异值。这是一个 hack(精度可能高于此方法确定的,并且结果可能应该被视为最坏情况的估计),但总比没有好。
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.
我也会担心返回值的准确性。在某些内核下,我希望在发生计时器 IRQ 时,对于碰巧正在运行的任何代码,计数器都会增加;因此,一个进程可能非常幸运(并且在计时器 IRQ 发生之前不断阻塞)或非常不走运(并且在计时器 IRQ 发生之前解除阻塞)。在这种情况下,“幸运”可能意味着 CPU hog 看起来没有使用 CPU 时间,而“不幸”可能意味着使用很少 CPU 时间的进程看起来像 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...
对于特定架构上特定内核的特定版本(在某些情况下,可能取决于内核是否/何时使用特定配置选项编译),可能有更高精度的替代方案,这些替代方案不可移植且不标准......
回答by Lars
You can check the real CPU time of a process on linux by utilizing the CPU Timefunctionality of the kernel:
您可以使用内核的CPU 时间功能检查 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 在进程上处理的实际指令量,从而获得实际的工作时间量。
回答by pooria
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
它将向您显示一段代码的执行时间