CSCI 3907: Seminar: Performance-Aware Programming

CSCI 3907: Profiling with the sample command on OS X


Unlike the UNIX gprof command, which you run after execution of your program completes, OS X's built-in sample command attaches to a currently-executing process (In this way it is like the more powerful PAPI performance tool we'll be working with later in the course). So, for our profiling exercise, you'll want to take your inefficient program (e.g., selection-sort) and increase the array size to a value big enough to take at least a minute to run (I used a value of one million). Once your inefficient program is running, open another terminal (via ⌘-n) and run the top command in that new terminal. You'll see a list of currently-executing programs, with your inefficient program at the top (hence the name of the command!), and its process ID (PID) number in the upper-left. Make a note of that PID, and issue the following command:

sample PID 5 > report.txt

where PID is the PID you got from top; for example:

sample 17493 5 > report.txt

This will do sampling on your program for five seconds, and put a gprof-style report into the file report.txt.