From charlesreid1

Shark is a profiler for Mac. It has a nice GUI interface.

Shark works much different from other profilers like Gprof. Basically, you run Shark, tell it to start gathering data, then you run your program like normal. Shark gathers information on every single process happening on the system.

It is arguable whether this is a good approach or not, but that's not really the point of this article.


Timing Profiles

You can use a timing profile to determine how much time is spent in various system and function calls. This is useful for developing a sense of which particular lines or functions in the code are taking most of the computational time.

Gathering Timing Data

Shark1.png

Start Shark and pick "Time Profile."

Shark2.png

When you press "Start," Shark will begin to gather data. This is when you run your program.

Shark3.png

Once you've finished gathering data, you can press "Stop" and Shark will start to process your data.

Shark4.png

After it finishes processing the data, it will show you all the different processes that were taking place on your system, and how much time each one took.

Shark5.png

Assuming you have compiled your binary with the -g (debug) flag, when you double-click on a particular item in the list, it will show you the source code, and the timing for each line of code.

The self column will tell you the percentage of time spent on that line out of the total time spent in that particular method. The total column will tell you the percentage of time spent on that line out of the total time spent on everything.

Shark6.png

Shark will also give you suggestions on how you can improve the performance of various bottlenecks in your code. These are often helpful, but sometimes not.