Profiling c++ on mac os x
Instruments is the tool to use. A full explanation of Instruments is outside the scope of this answer, but here's a quick start guide:
- Open Instruments.1
- Select the "Time Profiler" template.
- Select your application in the "Target" dropdown menu.2
- Hit the red circle ("record") button to start your application running.
- If applicable, do some stuff in your application that you need to profile.
- Hit the record button again to stop recording.
- Use the tools in Instruments to analyze your results.
Of the tools available, the ones that will be most frequently useful are:
- Expanding the call tree using the disclosure arrows
- Clicking the circled arrow on a function name to focus it
- Double-clicking a function to view the associated source
- The "Invert Call Tree" checkbox on the left-hand side
You can also start an invocation of instruments
on the command line:
instruments -l 30000 -t Time\ Profiler -p 5773
see instructions.
1 One easy way to open Instruments is to use Spotlight: Just click on the magnifying glass in the upper right corner of the taskbar (next to the clock) and type "Instruments".
2 Click "Choose Target..." and navigate to the path of your executable.
Instruments is the tool to use. To overcome the issue of the blank traces, make sure that you open Instruments from within XCode:
Xcode > Open Developer Tool > Instruments
If you open Instruments from an old Instruments icon that you pinned to your dock before the last update to XCode, it will give you blank traces.
Instruments really is the right answer, but if you can't figure out how to use it then another option is the profiler in the built-in Activity Monitor application. In Activity Monitor you can get info on any running process and there's a button to sample its execution for a while. You'll have to start your program, switch to Activity Monitor, find the process, and then sample it.
Additionally you can do 'poor man's profiling' simply by running the program in a debugger and pausing it manually half a dozen times or so and noting the call stack at those times. It's very simple but it works surprisingly well as a first pass for a significant fraction of programs.
There is also a command line sample
program which samples the callstack like the others do.