如何衡量Linux下C程序的实际执行时间? [英] How to measure the ACTUAL execution time of a C program under Linux?
问题描述
我知道这个问题可能已经被普遍问过,但似乎大多数的这些问题都是关于的过去的时间(基于挂钟)一片的code。在的过去的一块code的时间是不可能等于的实际的执行时间,其他进程可能会期间执行的过去的感兴趣的code的时间。
我用的getrusage()来获取进程的用户时间和系统时间,然后通过计算(用户时间+系统时间)的实际执行时间。我正在我在Ubuntu程序。这里是我的问题:
- 如何知道自己的getrusage的precision()?
- 是否有能提供更高的precision比的getrusage其他方法()?
您可以通过利用的CPU时间功能:
的#include<&time.h中GT; clock_t表示起点,终点;
双cpu_time_used; 开始=时钟();
... / *做的工作。 * /
结束=时钟();
cpu_time_used =((双)(结束 - 开始))/ CLOCKS_PER_SEC;
来源:<一个href=\"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
这样的话,你算CPU蜱或指令的实际量由CPU在工作的过程中,从而获得工作时间的实际数量。
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:
- How do I know the precision of getrusage()?
- Are there other approaches that can provide higher precision than getrusage()?
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.
这篇关于如何衡量Linux下C程序的实际执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!