Popular Tags

Who's online

There are currently 0 users and 61 guests online.

User login

It's sometime useful to see how long methods take to run so that we can see which ones we should focus on optimizing.

The most common approach to doing this is the following:

NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
[myObject doSomethingExpensive];
NSTimerInterval end = [NSDate timeIntervalSinceReferenceDate];
NSLog(@"Elapsed time: %f seconds", (end-start));

However, there are some issues with this. For example, +[NSDate timeIntervalSinceReferenceDate] relies on the system clock, which can be changed. For a more in-depth look at this issue, see this "Friday Q&A" by Mike Ash: http://www.mikeash.com/?page=pyblog/friday-qa-2009-11-13-dangerous-cocoa...

Aaron Hillegass (of Cocoa Programming for Mac OS X fame) recently posted his favorite approach of timing methods:

#import <mach/mach_time.h>
 
uint64_t start = mach_absolute_time();
 
// do stuff to be timed
 
uint64_t end = mach_absolute_time();
uint64_t elapsed = end - start;
 
mach_timebase_info_data_t info;
if (mach_timebase_info (&info) != KERN_SUCCESS) {
    printf ("mach_timebase_info failed\n");
}
 
uint64_t nanosecs = elapsed * info.numer / info.denom;
uint64_t millisecs = nanosecs / 1000000;

No votes yet

Search

UPDATES

The site has recently been updated. You may notice some of the following issues:

  • Some URLs no longer work. Please use the search box.
  • File uploads should now be working. If you experience problems, please contact us.