Profiling Your Code:
At some point in the semester (particularly if you choose to write an interactive raytracer), you will need to
identify bottlenecks in your program, i.e., what parts of your program take the most CPU time. This process is
called profiling your code, and there are various tools that can do this for you. Below are instructions
for using the GNU profiler ("gprof") to profile your code.
- Compile your program with the '-g' and '-pg' flags to gcc/g++.
- C/C++ files should be compiled as: g++ -c -g -pg -o myFile.o myFile.cpp
- Your program should be linked as: g++ -pg -o myExecutable *.o
- Run your program, with any necessary command line parameters, as usual.
- This will probably be much slower than normal, since profiling requires a non-optimized, debug executable.
- Your program must exit normally (e.g., by reaching the end of main() or by calling exit()).
- After your program finishes, there should be a file called "gmon.out" in your directory.
- Run the profiler on the output file "gmon.out".
- Run: gprof myExecutable > myProfile.txt
- Feel free to experiment with other options to tailor the output of gprof.
- Open up the file "myProfile.txt" so see your program's profile data.
Unfortunately, for those of you using Windows, I am unaware of any free profiler that works in Visual
Studio.NET. Microsoft claims that VS.NET 2005 includes a profiler, but my searching suggests this is only
included in the "Team Development Edition," which you probably don't have. My only suggestion is that you
write portable code, and compile under Linux (or Cygwin) so you can
profile with "gprof".
Last Modified: Wednesday, January 3, 2007
Chris Wyman (cwyman@cs.uiowa.edu)