Home

Welcome to Digital Ruby! We are a software company that specializes in Mobile Apps and Server Software.

Latest post:

Quick and dirty profiling for iOS

Apr 18, 2014

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];);



0 Comments

Recent articles…