Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
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
Posted
Comments
Albert Holguin 21-Nov-11 11:01am    
Not sure if there's really anything that will give you that fine of resolution to time each line. What is it that you're trying to accomplish by doing this?

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.
 
Share this answer
 
Comments
fjdiewornncalwe 21-Nov-11 11:15am    
I think we can safely assume that if the OP is to the point of being interested in execution times of "lines", they probably mean statements as you point out. But you do make a good point about clarity in descriptions.
Please consider this code :

C++
#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 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.
 
Share this answer
 
Comments
coffeenet 21-Nov-11 1:35am    
Thank you for the suggestion. But, I'm afraid it is not what I am looking for.
I need to see how much CPU time EACH line of code took.
thanks anyway
Amir Mahfoozi 21-Nov-11 1:49am    
Have you tried these ones :
http://valgrind.org/info/about.html
http://www.hpl.hp.com/research/linux/qprof/
http://en.wikipedia.org/wiki/List_of_performance_analysis_tools

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900