BNRTimeBlock
Timing how long a block of code takes is a useful tool. Maybe you’re choosing between two different calls that do similar things and you’re wondering which one is faster. If one is faster, is it faster-enough to make any difference? The usual techniques involve using a profiler like Instruments, or calling a time function like
gettimeofday()
before and after your code and calculating the delta. Those of us on mach-based systems like iOS and OS X can usemach_absolute_time()
, which is the finest-grained timepiece available on the system.
Instruments has its place, but I often prefer to the low-tech approach of adding logging and timing code directly to my app or unit tests. You can leave logging on while using the app normally, measure things on other people’s Macs, and keep a record of everything in version control.