Skip to content

Instantly share code, notes, and snippets.

@humblehacker
Last active Dec 14, 2015
Embed
What would you like to do?
==> 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