以下のマクロをPrefix.pchに追加しておくと、ちょっとした最適化をしたいときに簡単に処理時間を測れます。
ただし、結果にはばらつきがあるのと、精度が<1msのようですので、あくまで目安です。
マクロ
# ifdef DEBUG
# define TIMING_START NSDate *timingStartDate = [NSDate date];
# define TIMING_LOG(message) NSLog(@"[%@] elapsed time: %f msec", message, [[NSDate date] timeIntervalSinceDate:timingStartDate] * 1000);
# else
# define TIMING_START ;
# define TIMING_LOG(message) ;
# endif
サンプル
Sample.m
NSDictionary *dictionary = @{ @"name" : @"tarou", @"age" : @20, @"gender" : @"male" };
NSMutableString *description = [[NSMutableString alloc] init];
TIMING_START;
# if 1
NSEnumerator *enumerator = [dictionary keyEnumerator];
NSString *key;
while (key = [enumerator nextObject]) {
[description appendFormat:@"%@は%@。", key, dictionary[key]];
}
# else
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[description appendFormat:@"%@は%@。", key, obj];
}];
# endif
NSString *message = [NSString stringWithFormat:@"finish: %@", description];
TIMING_LOG(message);
2014-06-04 20:03:30.296 MyApp[181:60b] [finish: nameはtarou。ageは20。genderはmale。] elapsed time: 0.109017 msec
2014-06-04 20:06:06.984 MyApp[204:60b] [finish: nameはtarou。ageは20。genderはmale。] elapsed time: 0.126958 msec