Try using gcov -
If you want to find out where all your time is going, then gcov is for you. To use gcov you compile with some flags on, then run your program, then run gcov on the source file, it will create a new file that is the source file name with '.gcov' at the end. This file will tell you how many times each line ran. The compiler flags to use gcov are -fprofile-arcs -ftest-coverage. If you run gcov with a '-b' it will report percentages on every condition evaluated. There is another program gprof, but it doesn't give as much information.
This is no man page for gcov, so try this web site if you want more info:
gcov info from http://gcc.gnu.org/
Some compiler tricks you should know :
When you are all done and know everything works, you can still speed up your program. You add these to your Makefile when you build the final version of the program.