Difference between revisions of "Profiling"

From SourceWiki
Jump to navigation Jump to search
Line 34: Line 34:
 
cd profiling/examples/example1
 
cd profiling/examples/example1
 
make
 
make
make run
+
valgrind --tool=callgrind ./div >& div.out
 +
kcachegrind callgrind.out.xxxx
 
</pre>
 
</pre>
 +
 +
We can see from the graphical display given by kcachegrind that our inefficient division routine takes far more of the runtime that our efficient routine.  Using this kind of information, we can focus our re-engineering efforts on the slower parts of our program.
 +
 +
[[Image:callgrind.JPG|center|Profile information displayed graphically in kcachegrind]]

Revision as of 16:49, 11 November 2008

Can I speed up my program?

Introduction

How fast does each part of my program take? Perhaps I could speed it up? As with debugging, we could add print statements, use a stopwatch, compile and re-run etc. etc. But that would be tedius and there are tools around which do the job way better.

Compiler Options

You probably want to make sure that you've added all the go-faster flags that are available before you embark on any profiling. Activating optimisations for speed can make your progam run a lot faster!

Using Valgrind

Valgrind is an excellent open-source tool for debugging and profiling.

Compile your program as normal, adding in any optimisation flags that you desire and also -g. Then run it through the callgrind tool, embedded in valgrind:

valgrind --tool=callgrind your-program [program options]

When your program has run you find a file called callgrind.out.xxxx in the current directory, where xxxx is replaced with a number (the process ID of the command you have just executed).

You can inspect the contents of this newly created file using a graphical display too call kcachegrind:

kcachegrind callgrind.out.xxxx

An Example

svn co http://source.ggy.bris.ac.uk/subversion-open/profiling ./profiling
cd profiling/examples/example1
make
valgrind --tool=callgrind ./div >& div.out
kcachegrind callgrind.out.xxxx

We can see from the graphical display given by kcachegrind that our inefficient division routine takes far more of the runtime that our efficient routine. Using this kind of information, we can focus our re-engineering efforts on the slower parts of our program.

Profile information displayed graphically in kcachegrind