In every project comes the time where you need to save a couple of cycles here and a couple of cycles there in order to improve performance. Often it is pretty easy to judge just by eyeballing it which approach will result in better performance for most cases. But sometimes that can be a little bit trickier and we (as engineers) want to have solid data – numbers – to back our judgement.
I’ve always found it tedious to write the ever-repeating timing-analysis functionality and insert into my code only to have to remove it ten minutes later after I have collected the results of my benchmarks. I won’t bore you with the old way I did this and rather just share my recently written little helper class. Heck, basically it is the old approach only encapsulated in an external class.
You have three methods to choose from. Each takes a block as an argument and records its execution time for you.
timeForBlock: just returns the execution time in milliseconds,
printTimeForBlock: prints that time and
printTimeForBlock:withTitle: prints that time plus a title. Pretty simple – no fluff. Just a tiny little helper. Enjoy!