Created
November 23, 2012 15:19
-
-
Save yas375/4136100 to your computer and use it in GitHub Desktop.
Measuring the execution time of some code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mach/mach_time.h> | |
void RecordPerformanceMetricks(uint64_t start, uint64_t end); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "DebugHelpers.h" | |
void RecordPerformanceMetricks(uint64_t start, uint64_t end) { | |
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ | |
//////////////////////////////////////////////////////////////////// | |
// https://developer.apple.com/library/mac/#qa/qa1398/_index.html // | |
//////////////////////////////////////////////////////////////////// | |
static mach_timebase_info_data_t sTimebaseInfo; | |
uint64_t elapsed = end - start; | |
// Convert to nanoseconds. | |
// If this is the first time we've run, get the timebase. | |
// We can use denom == 0 to indicate that sTimebaseInfo is | |
// uninitialised because it makes no sense to have a zero | |
// denominator is a fraction. | |
if ( sTimebaseInfo.denom == 0 ) { | |
(void) mach_timebase_info(&sTimebaseInfo); | |
} | |
// Do the maths. We hope that the multiplication doesn't | |
// overflow; the price you pay for working in fixed point. | |
uint64_t elapsedNano = elapsed * sTimebaseInfo.numer / sTimebaseInfo.denom; | |
///////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////// | |
NSLog(@"%lli ms", elapsedNano / 1000000); // I would like milliseconds | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "DebugHelper.h" | |
@implementation FFMyClass | |
- (void)someMethod { | |
uint64_t drawStart = mach_absolute_time(); | |
// something goes here... | |
uint64_t drawEnd = mach_absolute_time(); | |
RecordPerformanceMetricks(drawStart, drawEnd); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Especially interested: am I right with using
dispatch_sync
andNSLog
? Any other suggestion and improvements?Thanks.