Thursday, June 10, 2010

iPhone optimization -- timing your code

Direct C calls are the best in the iPhone the below test doesn't account for the time to release the date object as well but it clearly shows which is the better approach.

CFAbsoluteTime profile_start = CFAbsoluteTimeGetCurrent();

for(int i = 0; i < 100000; i++)
  CFAbsoluteTimeGetCurrent();
NSLog(@"direct C call: %0.5f", CFAbsoluteTimeGetCurrent() - profile_start);

profile_start = CFAbsoluteTimeGetCurrent();
for(int i = 0; i < 100000; i++)
  [NSDate date];

NSLog(@"NSDate call: %0.5f", CFAbsoluteTimeGetCurrent() - profile_start);

The output is;
direct C call: 0.01097
NSDate call: 0.05005

No comments:

Post a Comment