LoginSignup
6
6

More than 5 years have passed since last update.

処理時間簡易計測マクロ

Last updated at Posted at 2014-06-04

以下のマクロを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

精度について

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6