Tuesday, July 3, 2012

It’s the time to speed-up your code with "gprof"


Abstract:

"Perfect in performance" will be a challengeable task, even may be a nightmare to the 
application developer! As per the studies, most of the developers put their much effort to find out
where their code lags…
Profiling allows you to understand where your program spent its time and which functions called
which other functions while it was executing. It can also tell you which functions are being called
more or less often than you expected.  This information can show you which pieces of your
program are slower than you expected, and might be developers for rewriting to make your
program execute faster. This may help you spot bugs that had otherwise been unnoticed also.
Meet "gprof", a free profiling tool provided by GNU Foundation and experience how you can
change your nightmare to "noon-hallucination”!

Introduction:

"Embedded system" always demands performance first than anything else. No matter how 
complex or simple the application may, but performance will give an up-stand to that in the 
market. This may be the primary reason, why every developer spending lot of time to make 
codes perfect than write a code!
But, the process of making a code perfect is not easy as you might think when it comes to
manual correction. Most efforts become equivalent to counting the stars on the sky, when the 
reason behind the execution lagging is unknown.
It is therefore very clear that a tool, which can go inside the code and find out the real factors 
behind lagging, can help in vast. “Code-profiling” does this and gprof is a good tool to start 
with…

How to start with profiling?

gprof profiling can be enable by add -pg to the gcc compile flags. 
Example as follows:

gcc test.c -pg -o test -O2 –lc

Once the program is compiled for profiling, you must run it in order to generate the information 
that gprof needs. Once you have built the application, simply run it as normal, using the normal 
arguments, file names, etc. as follows:

. /test 50000

The program should run normally, producing the same output as usual. It will, however, run
somewhat slower than normal because of the time spent collecting and the writing the profile 
data.
Once this completes, you should see a file called gmon.out created in the current directory.
In order to write the "gmon.out" file properly, your program must exit normally, by returning 
from main or by calling exit. Calling the low-level function _exit does not write the profile data, 
and neither does abnormal termination due to an unhandled signal.

Interpreting gprof’s Output:

gprof can produce several different output styles, the most important of which are described 
below.

1. The Flat Profile:

Flat profile can be obtained with the gprof command, passing the executable itself and the 
gmon.out file as follows:

gprof test gmon.out -p

The flat profile shows how much time your program spent in each function, and how many 
times that function was called. If you simply want to know which functions burn most of the 
cycles, it is stated concisely here.
Output:

2. The Call Graph:

The call graph shows, for each function, which functions called it, which other functions it 
called, and how many times. There is also an estimate of how much time was spent in the 
subroutines of each function. This can suggest places where you might try to eliminate function 
calls that use a lot of time.
"Call graph" can be obtain as follows:

gprof  test gmon.out -q

Output:

Conclusion:

"gprof" is free yet very powerful profiling tool for optimizing your code. Profiler uses 
information collected during the actual execution of your program. So, it can be used on 
programs that are too large or too complex to analyze by reading the source. However, how your 
program is run will affect the information that shows up in the profile data. If you don‟t use some 
feature of your program while it is being profiled, no profile information will be generated for 
that feature.
One of the main advantages of gprof over the other profiling tools is that gprof will be common 
in Linux machine and it preinstalled with "gcc" in most of the system.
What are you waiting for then? Get the advantage of the free GNU profiler from today. Life 
makes easy…


Find more about gprof on GNU gprof manual:



No comments: