Instruments is an amazing tool for finding all sorts of performance issues in software, but sometimes, all you want to do is log a quick and dirty timing of a snippet of code.
Mark Darlymple once posted a quickie that does just this. Unfortunately, the code isn’t very reusable. You have to wrap each chunk of code you want to profile with nearly 8 lines of boilerplate code.
We can reduce some of this complexity by writing a profile
function that takes a block of work as an argument instead.
Now, all we have to do is call profile
and pass in the block we wish to time; the timing will get logged to standard out.