I’ve seen Q/A here about timing function duration in other languages but didn’t find anything in Objective-C. If there is one, post the link and I will delete this question
I want to measure the time it takes for some functions to run. I’ve cobbled together the bits of code below but I wonder if there is a more compact or portable way to do this.
CFTimeInterval startTime = CFAbsoluteTimeGetCurrent();
// do some work
CFTimeInterval difference = CFAbsoluteTimeGetCurrent() - startTime;
printf("elapsed: %f\n", difference)
If you want to precisely time very short timespans there are two additional timing facilities with very low overhead that come to my mind:
mach_absolute_timewith nanosecond resolution__builtin_readcyclecounter: a clang builtin function to read a low-latency, high-accuracy clock.