I have a program made up of several .h and .c files and a lot of functions. And there are functions which call other functions and so on.
Now, this is actually an assignment so I know how much time the program needs to reach the end.
The problem is, my program takes too much time compared to the times I am given.
Is it possible to find out which function is taking too much time or which part of the code is holding the program down?
I did not give the code here because it is too long. I know that no one can answer why “my program” is slow but I am talking in general!
Is there a tool that measures how much time each function takes or something similar?
I am using gcc and I’m on Linux.
Since you are on linux, you probably have the
gprofprofiler installed already. The most basic use ofgprofis by compiling with the-pgoption (the-goption is also needed to get informative output). e.g.Now, you can just run your program normally. Then you run
which will output the profiling information into
profile.txt. This data looks a little likeand you can read off some data about each function (e.g.
openwas called 7208 times and 0.02s were spent executing it.). That example data was borrowed from this guide, which you should read as it gives much more explanation and describes to how manipulate the profiling to get things like line-by-line profiling.