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
==> MachTimer.h <== | |
// | |
// MachTimer | |
// Tout | |
// | |
// Code sourced from http://zpasternack.org/high-resolution-timing-in-cocoa/ | |
// | |
#import <Foundation/Foundation.h> | |
#include <mach/mach_time.h> | |
@interface MachTimer : NSObject | |
+ (id) timer; | |
- (void) start; | |
- (uint64_t) elapsed; | |
- (Float64)elapsedSeconds; | |
@end | |
==> MachTimer.m <== | |
// | |
// MachTimer | |
// Tout | |
// | |
// Code sourced from http://zpasternack.org/high-resolution-timing-in-cocoa/ | |
// | |
#import "MachTimer.h" | |
static mach_timebase_info_data_t timeBase; | |
@implementation MachTimer | |
{ | |
uint64_t timeZero; | |
} | |
+ (void) initialize | |
{ | |
(void) mach_timebase_info( &timeBase ); | |
} | |
+ (id) timer | |
{ | |
#if( __has_feature( objc_arc ) ) | |
return [[[self class] alloc] init]; | |
#else | |
return [[[[self class] alloc] init] autorelease]; | |
#endif | |
} | |
- (id) init | |
{ | |
if( (self = [super init]) ) { | |
timeZero = mach_absolute_time(); | |
} | |
return self; | |
} | |
- (void) start | |
{ | |
timeZero = mach_absolute_time(); | |
} | |
- (uint64_t) elapsed | |
{ | |
return mach_absolute_time() - timeZero; | |
} | |
- (Float64)elapsedSeconds | |
{ | |
return (Float64)(mach_absolute_time() - timeZero) * (Float64)timeBase.numer / (Float64)timeBase.denom / (Float64)NSEC_PER_SEC; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment