Skip to content

Instantly share code, notes, and snippets.

@yas375
Created November 23, 2012 15:19
Show Gist options
  • Save yas375/4136100 to your computer and use it in GitHub Desktop.
Save yas375/4136100 to your computer and use it in GitHub Desktop.
Measuring the execution time of some code
#include <mach/mach_time.h>
void RecordPerformanceMetricks(uint64_t start, uint64_t end);
#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
});
}
#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
@yas375
Copy link
Author

yas375 commented Nov 23, 2012

Especially interested: am I right with using dispatch_sync and NSLog? Any other suggestion and improvements?

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment