The other answers are correct (with a caveat*). I add this answer simply to show an example usage:
- (void)getYourAffairsInOrder
{
NSDate* methodStart = [NSDate date]; // Capture start time.
// … Do some work …
NSLog(@"DEBUG Method %s ran. Elapsed: %f seconds.", __func__, -([methodStart timeIntervalSinceNow])); // Calculate and report elapsed time.
}
On the debugger console, you see something like this:
DEBUG Method '-[XMAppDelegate getYourAffairsInOrder]' ran. Elapsed: 0.033827 seconds.
*Caveat: As others mentioned, use NSDate
to calculate elapsed time only for casual purposes. One such purpose might be common testing, crude profiling, where you just want a rough idea of how long a method is taking.
The risk is that the device's clock's current time setting could change at any moment because of network clock syncing. So NSDate
time could jump forward or backward at any moment.