如何获得“每行代码的执行时间"?为我的程序? [英] How to get "execution time for each line of code" for my program?
问题描述
我只是用gprof分析我的程序.我想看看哪些功能消耗了最多的CPU时间.
但是,现在我想以另一种方式分析我的程序.我想看看哪些代码行占用最多的CPU时间.
起初,我读到gprof可以做到,但是我找不到适合的选项.
现在,我找到了gcov.但是,我尝试执行的第三方程序没有"./configure".因此我无法应用"./configure --enable-gcov".
我的问题很简单.有谁知道如何为我的程序的每一行代码获取执行时间?
(我更喜欢gprof的建议,因为我发现它的输出非常易于阅读和理解.)
问候,
Linux noob
Hi,
I just used gprof to analyze my program. I wanted to see what functions were consuming the most CPU time.
However, now I would like to analyze my program in a different way. I want to see what LINES of the code that consume the most CPU time.
At first, I read that gprof could do that, but I couldn''t find the right option for it.
Now, I found gcov. However, the third-party program I am trying to execute has no "./configure" so I could not apply the "./configure --enable-gcov".
My question is simple. Does anyone know how to get execution time for each line of code for my program?
(I prefer suggestions with gprof, because I found its output to be very easy to read and understand.)
Regards,
Linux noob
推荐答案
问题是编译器不关心在源代码中放置换行符的位置.它会看到您的代码包含声明 [基本块 [ ^ ],而不是一行. 每行的执行时间"的整个想法几乎毫无意义.
The problem is that the compiler doesn''t care about where you put line feeds in your source code. It sees your code consisting of statements[^] and basic blocks[^], not lines. The whole idea of "execution time of each line" is nearly meaningless.
请考虑以下代码:
Please consider this code :
#include
#include <stdio.h>
#include <unistd.h>
int main()
{
struct timeval start, end;
long mtime, seconds, useconds;
gettimeofday(&start, NULL);
ProfiledFunction();
gettimeofday(&end, NULL);
seconds = end.tv_sec - start.tv_sec;
useconds = end.tv_usec - start.tv_usec;
mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
printf("Elapsed time: %ld milliseconds\n", mtime);
return 0;
}
</unistd.h></stdio.h>
ProfiledFunction
是正在分析的功能.
代码是从这里提取的:
http://stackoverflow.com/Questions/588307/c-obtaining-milliseconds-time-on-linux-clock-doesnt-seem-to-work-properl [ http://kerneltrap.org/node/6393 [
ProfiledFunction
is the function which is being profiled.
The code was extracted from here :
http://stackoverflow.com/questions/588307/c-obtaining-milliseconds-time-on-linux-clock-doesnt-seem-to-work-properl[^]
If you need microsecond precision just use tv_usec
part of timeval
structure.
If you''re developing in kernel mode please consider using this function :
http://kerneltrap.org/node/6393[^]
Hope it helps.
这篇关于如何获得“每行代码的执行时间"?为我的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!