Quick and dirty profiling for iOS
I was tracking down some nasty bits of code today, code which when called once wasn’t noticeably slow, but when called thousands of time started to really add up.
Here are the macros I made:
#define CREATE_TIMER NSTimeInterval start, stop #define START_TIMER start = [NSDate timeIntervalSinceReferenceDate] #define CREATE_AND_START_TIMER CREATE_TIMER; START_TIMER #define END_TIMER(msg) stop = [NSDate timeIntervalSinceReferenceDate]; NSLog(@"%@", [NSString stringWithFormat:@"%@ Time = %f", msg, stop-start]) #define PROFILE(code) static CGFloat s_totalTime = 0.0f; static long s_callCount = 0; s_callCount++; CREATE_AND_START_TIMER; code s_totalTime += ([NSDate timeIntervalSinceReferenceDate] - start); NSString* timerMsg = [NSString stringWithFormat:@"PROFILE, total time: %f, call count: %ld", s_totalTime, s_callCount]; END_TIMER(timerMsg); Usage: PROFILE([self runCodeToBeProfiled];);
I hope you find this macro useful as you profile iOS apps.