This guide is modified from section (d) of the worksheet inside Module 10 of Phys 6810 Computational Physics (Spring 2023).
NOTE: gprof does not work on macOS. Please use a linux machine (such as OSC)
To use gprof, compile and link the relevant codes with the -pg option:
Take a look at the Makefile make_hello_world and modify both the CFLAGS and LDFLAGS lines to include -pg
Compile and link the script by typing
make -f make_hello_world
Execute the program
./hello_world.x
With the -pg flags, the execution will generate a file called gmon.out that is used by gprof.
The program has to exit normally (e.g. we can't stop with a ctrl-C).
Warning: Any existing gmon.out file will be overwritten.
Run gprof and save the output to a file (e.g., gprof.out) by
gprof hello_world.x > gprof.out
We should at this point see a text file called gprof.out which contains the profile of hello_world.cpp
vim gprof.out |