Reputation: 18517
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)
Upvotes: 0
Views: 540
Reputation: 81878
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_time
with nanosecond resolution__builtin_readcyclecounter
: a clang builtin function to read a low-latency, high-accuracy clock.Upvotes: 3
Reputation: 9578
No,This is the best way using CFAbsoluteTimeGetCurrent(); its Absolute time is measured in seconds relative to the absolute reference date of Jan 1 2001 00:00:00 GMT
Upvotes: 3