This is an old revision of the document!
Table of Contents
Profiling
A collection of code profiling techniques follows.
Dynamic Memory Allocation
The biggest problems with dynamic memory management are:
- leaks and
- corruption.
While the later is rather tricky to analyse, for memleaks there is
valgrind
. Invocation as follows:
$ gcc -g test.c $ valgrind --leak-check=full ./a.out
Performance
When programming, the code complexity (O-notation) is the main factor
identifying CPU-intense algorithms. Reducing the code's complexity often
doesn't suffice, though. E.g. IO-intense operations often lead to delays at
run-time which isn't covered by the O-notation, at all. This means that aside
of complexity analysis, there always should be run-time code execution time
measurement. And this is where gprof
comes into action:
$ gcc -pg -g test.c $ ./a.out $ gprof a.out gmon.out
Coverage
Code coverage means to have a look at how often each line of code is being
executed. Interestingly, this can be used to “measure” the code's complexity.
This way a broader view over the problem can be provided, e.g. answering the
question if a seldomly executed O(n) algorithm is heavier than an O(log(n))
one being executed all the time. One of the best tools to analyse the code
coverage is gcov
:
$ gcc -fprofile-arcs -ftest-coverage test.c $ ./a.out $ gcov tmp.c
this will create the file test.c.gcov containing the annotated source code of test.c.