Click here to Skip to main content
14,300,703 members
Rate this:
Please Sign up or sign in to vote.
See more:
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.)

Linux noob
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?
Rate this:
Please Sign up or sign in to vote.

Solution 3

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.
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.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Please consider this code :

#include <stdio.h>
#include <unistd.h>
int main()
    struct timeval start, end;

    long mtime, seconds, useconds;    

    gettimeofday(&start, NULL);
    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;


ProfiledFunction is the function which is being profiled.
The code was extracted from here :[^]

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 :[^]

Hope it helps.
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 :

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100