On macOS, you can use Instruments (part of XCode, but it can also be used as a 
standalone app) to do time profiling.

On macOS Sierra or later, you can run it from the commandline using:
    
    
    instruments -l 10000 -D output.trace -t "Time Profiler" /path/to/executable 
args...
    
    
    Run

(On older versions of macOS, the `iprofiler` command does the same thing, 
though with different options; for those, see `iprofiler --help` or `man 
iprofiler`. You can also run programs from the Instruments UI, but that is more 
cumbersome to set up.)

The `-l` option is for the maximum number of seconds the program may run (after 
that, it will be killed), the `-D` option specifies the output directory for 
the trace, and the `-t` option allows you to choose what kind of analysis to 
run.

Then `open output.trace` to be able to inspect the profile in the Instruments 
UI. You can see a sample screenshot of the UI with the output from the original 
code [here](https://i.imgur.com/zHMk2Gz.png). Don't forget to invert the call 
tree in the UI (using the button at the bottom of the window), it gives you a 
much more useful breakdown of where time is spent.

Reply via email to